【问题标题】:Unable to return token from AsyncStorage in React Native无法从 React Native 中的 AsyncStorage 返回令牌
【发布时间】:2016-05-31 10:13:54
【问题描述】:

我正在使用 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);
  }

^^^^这个函数成功保存了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) 时无法返回值。如何从 Promise 中返回值?

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

    • 我也不认为你需要拨打.done()电话。
    猜你喜欢
    • 2018-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-16
    相关资源
    最近更新 更多