无法从 React Native 中的 AsyncStorage return 令牌

Unable to return token from AsyncStorage in React Native

我正在使用 React Native 的 AsyncStorage 来存储授权令牌,当需要用于新的 http 请求时,它将被 returned。虽然我可以成功地存储它并在控制台记录它,但我在 return 计算该值时遇到了一些麻烦。我想在另一个 window 中以

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

然而它不会起作用。

 LocalDb.getAccessToken();

另一方面,当 getAcessToken() 中有 console.log 时有效

exports.storeToken=function(token){
  AsyncStorage.setItem('access_token', 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) 时,我无法 return 值。我怎样才能 return 来自 promise 的值?

您需要 return getAccessToken 函数调用 promise。我也会 return 具有价值的承诺...像这样。

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