无法从React Native

时间:2016-05-31 10:13:55

标签: authentication react-native token es6-promise asyncstorage

我正在使用React Native的AsyncStorage来存储授权令牌,当需要用于新的http请求时,将会返回该令牌。虽然我可以成功存储它并且控制台记录它,但是我在返回值时遇到了一些麻烦。我想以另一种方式在

的方式中调用它
 var x= LocalDb.getAcessToken();
 console.log(x);

然而它赢了,不行。

 LocalDb.getAccessToken();

另一方面,当getAcessToken()中包含console.log时,它可以正常工作

exports.storeToken=function(token){
  AsyncStorage.setItem('access_token', token);
  }

^^^^此功能成功保存令牌

exports.getAccessToken=function(){
   AsyncStorage.getItem('access_token')
   .then((value) => {
       if(value) {
        console.log(value);
    **//I want to return the value here, to use in another function**
      }
    })
  .done();
}

当我使用return(value)时,我无法返回值。如何从承诺中返回一个值?

1 个答案:

答案 0 :(得分:2)

您需要使用promise调用返回getAccessToken函数。我也会回复带有价值的承诺......就像这样。

exports.getAccessToken=function(){
 return AsyncStorage.getItem('access_token')
 .then((value) => {
     if(value) {
      console.log(value);
      return value;
    }
   })
}