1
function getUserID(externalEmail) {
        fetch("https:....SOME HOST" + "+'" + externalEmail + "'", {
                method:'GET',
                    headers:{
                        'Content-Type':'application/json',
                        'Some Username field':'<username>',
                        'Some Password field':'<password>'
                    },

        })
          .then(function(response) {
            return response.json()
        }).then(function(json) {
            console.log(json.result[0].id)
            return json.result[0].id
        }).catch(error = > console.log(error));
    }

I want to return the id to this function getUserID so i can store it in a variable, something like.

var usersID = getUserID()

I keep getting undefined, i can only see the ID when i console.log(). PS. i specify the userEmail before the function getUserID

If anyone can help that will be much appreciated.

My latest Change....

function delay(){
    return new Promise(function(resolve, reject){
    var email = "@email.com"
    setTimeout(function(){
    resolve(fetch("https:....SOME HOST" + "+'" + externalEmail + "'", {
                method:'GET',
                    headers:{
                        'Content-Type':'application/json',
                        'Some Username field':'<username>',
                        'Some Password field':'<password>'
                    },

              }))
             } ,1000);
   });
}
delay()
      .then(function(response){
          return response.json()
      }).catch(error => console.log(error))

Joshua
  • 1,005
  • 1
  • 11
  • 16
  • 3
    read on async/await. You cannot directly assign the return of getUserID() since it is asynchronous. You want to hold the assignment until the promise resolves to a value. You can do that by using the keyword "await". – Dhananjai Pai Jan 04 '21 at 10:22
  • Try returning the fetch inside `getUserID`. It can then be awaited or the value can be used inside a `then`. – evolutionxbox Jan 04 '21 at 10:27
  • You forgot return before fetch – Aluan Haddad Jan 04 '21 at 10:35
  • @DhananjaiPai thank you for your comment, can you provide a small example for me please, i read about async and added async before the function, but reach a problem i have seen that i am not sure how to solve,"promise {undefined}" – Joshua Jan 04 '21 at 10:36
  • thank you @evolutionxbox when i add return i now see promise { } which i have also seen but am confused on how to solve this. – Joshua Jan 04 '21 at 10:39
  • thank you @AluanHaddad my previous comment is also for you – Joshua Jan 04 '21 at 10:39
  • 1
    Does this answer your question? [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Aluan Haddad Jan 04 '21 at 10:42
  • 1
    Thanks @AluanHaddad Just had a look at that, thank you, I will try to use a promise and understand how it works – Joshua Jan 04 '21 at 11:16
  • @DhananjaiPai i have changed my code and now it is pending, do you know much about this. I will add my code now – Joshua Jan 04 '21 at 12:19
  • 1
    You simply cannot take a promise and use that object as anything meaningful. You must use the result in the `then` function, or use the `await` keyword (for those environments with global await facility) Read the answer(s) to the question posted by @AluanHaddad closely. – Heretic Monkey Jan 04 '21 at 12:32
  • @HereticMonkey Thank you, once i re-read that post, is storing a specific variable from the result, lets say result is result[name: "James", id: "123"] will i be able to return the id to the function ```getUserID``` and store it into a global variable through the use of ```then``` – Joshua Jan 04 '21 at 12:49

0 Answers0