【发布时间】:2017-09-17 11:59:57
【问题描述】:
当我等待收到关于是否应该跳过介绍的响应时,它会使用我在开头设置的值运行 if 语句。下面是代码:
componentWillMount(){
console.log("Intro component will mount:");
let skipIntro = false;
this.checkSkipIntro()
.then((response)=>{
skipIntro=response;
console.log("skipIntro is inside Async: " + skipIntro);
});
console.log("skipIntro is: " + skipIntro);
if(skipIntro){
console.log("skipping Intro");
Actions.login();
}
}
async checkSkipIntro() {
let skipIntro = await AsyncStorage.getItem("skipIntro");
console.log("Skip Intro: " + skipIntro);
if (skipIntro === "yes"){
console.log("returning True");
return true;
}
else{
console.log("returning False");
return false;
}
}
日志打印出来:
Intro.js:9 Intro component will mount:
Intro.js:16 skipIntro is: false
Intro.js:37 Skip Intro: yes
Intro.js:39 returning True
Intro.js:14 skipIntro is inside Async: true
如果您在调试器中注意到,console.log("skipIntro is: " + skipIntro);
在 Intro.js:16 在 console.log("skipIntro is inside Async: " + skipIntro); 之前运行
在 Intro.js:14。我不确定如何暂停代码的执行,直到函数返回适当的值。
我希望日志输出:
Intro.js:9 Intro component will mount:
Intro.js:14 skipIntro is inside Async: true
Intro.js:37 Skip Intro: yes
Intro.js:39 returning True
Intro.js:16 skipIntro is: true
【问题讨论】:
标签: javascript reactjs asynchronous react-native async-await