最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I access a variable outside a promise `.then` method? - Stack Overflow

programmeradmin1浏览0评论

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.

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.

Share Improve this question edited Nov 29, 2017 at 22:07 georgeawg 49k13 gold badges77 silver badges98 bronze badges asked Nov 29, 2017 at 18:55 Rodolfo RRodolfo R 4471 gold badge5 silver badges17 bronze badges 1
  • I'm guessing you have return names, but you also need return Spotify.getCurrentUser()... so that it returns the promise (and ultimately the return value from the then) – Danny Commented Nov 29, 2017 at 18:59
Add a comment  | 

2 Answers 2

Reset to default 11

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
   });

If you use it like that you can use the await call

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;
        });
    }
}

const names = await getUser();
发布评论

评论列表(0)

  1. 暂无评论