Javascript 解析ComponendImount中的异步存储承诺
我正在检查用户是否经过身份验证。我通过检查异步存储中的一些记录来实现这一点,我有以下代码 App.jsJavascript 解析ComponendImount中的异步存储承诺,javascript,react-native,ecmascript-6,jsx,Javascript,React Native,Ecmascript 6,Jsx,我正在检查用户是否经过身份验证。我通过检查异步存储中的一些记录来实现这一点,我有以下代码 App.js let AuthService = require('./app/layouts/AuthService/AuthService.js'); export default class App extends React.Component { componentDidMount() { AuthService.getAuthInfo((err, authInfo) =
let AuthService = require('./app/layouts/AuthService/AuthService.js');
export default class App extends React.Component {
componentDidMount() {
AuthService.getAuthInfo((err, authInfo) => {
this.setState({
checkingAuth: false,
isLoggedIn: authInfo != null
})
});
}
}
Auth.js
'use strict';
let AsyncStorage = require('react-native').AsyncStorage;
let _ = require('lodash');
const authKey = 'auth';
const userKey = 'user';
class AuthService {
getAuthInfo(cb){
AsyncStorage.multiGet([authKey, userKey], (err, val)=> {
if(err){
return cb(err);
}
if(!val){
return cb();
}
let zippedObj = _.zipObject(val);
if(!zippedObj[authKey]){
return cb();
}
let authInfo = {
header: {
Authorization: 'Basic ' + zippedObj[authKey]
},
user: JSON.parse(zippedObj[userKey])
}
return cb(null, authInfo);
});
}
}
module.exports = new AuthService();
在app.js中,我试图从Auth.js使用此函数,但我没有从该函数得到响应,在进入AsyncStorage函数之前,我从getAuthInfo获得控制台日志。我对react native和ES6非常陌生,我认为这是一个承诺或异步问题,但我不能让它工作。在app.js中,我重新定义了一个活动指示器,这样我就不会用checkingAuth和isLoggedIn来阻止用户界面。
我尝试使用一些。然后在app.js中没有结果。首先,返回回调函数而不是调用它。尝试通过如下方式删除
return
来调用它:cb(null,authInfo)代码>