Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I access a variable outside a promise `.then` method?

I'm working on a Spotify app. I'm able to login and get my token. My problem is I cannot access a variable outside the method. In this case "getCurrentUser"

This is my method:

function getUser() {
  if ($localStorage.token == undefined) {
    throw alert("Not logged in");
  } else {
    Spotify.getCurrentUser().then(function(data) {
      var names = JSON.stringify(data.data.display_name);
      console.log(names)
    })
  }
};

As you can see I console.logged the name and I do get the right value in the console. But only works there if I call the function getUser() I get undefined even with a return of the names variable.

I need to $scope that variable.

like image 759
Rodolfo R Avatar asked Nov 29 '17 18:11

Rodolfo R


People also ask

How do you access a variable outside a function?

To access a variable outside a function in JavaScript make your variable accessible from outside the function. First, declare it outside the function, then use it inside the function. You can't access variables declared inside a function from outside a function.

How do you get a variable from a promise?

Define an async function. Use the await operator to await the promise. Assign the result of using the await operator to a variable.

Is it possible to access the data outside the subroutine?

You can not access the variables inside the function unless it is declared outside it.

How do I wait for a promise to finish before returning the variable of a function?

Use of setTimeout() function: In order to wait for a promise to finish before returning the variable, the function can be set with setTimeout(), so that the function waits for a few milliseconds. Use of async or await() function: This method can be used if the exact time required in setTimeout() cannot be specified.


1 Answers

getUser() is not returning anything. You need to return the promise from the Spotify.getCurrentUser(), and then when you return names within that it is returned by the outer function.

function getUser() {

    if ( $localStorage.token == undefined) {
        throw alert("Not logged in");
    }
    else {
        return Spotify.getCurrentUser().then(function(data) {
            var names = JSON.stringify(data.data.display_name);
            console.log(names)
            return names;
        })
    }
}

The above answered why you were getting undefined when calling getUser(), but if you want to work with the end result you also want to change how you're using the value you get from getUser - it returns a promise object, not the end result you're after, so your code wants to call the promise's then method when the promise gets resolved:

getUser()                        // this returns a promise...
   .then(function(names) {       // `names` is the value resolved by the promise...
      $scope.names = names;      // and you can now add it to your $scope
   });
like image 127
Danny Avatar answered Oct 06 '22 00:10

Danny