[英]Pause execution of code until Asynchronous function returns the desired value
While I am waiting to receive a response on whether or not I should skip the intro, it runs the if statement with the value I set at the beginning. 当我等待收到有关是否应跳过简介的响应时,它将使用开头设置的值运行if语句。 Below is the code:
下面是代码:
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;
}
}
The log prints out: 日志打印出来:
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
If you notice in the debugger, the console.log("skipIntro is: " + skipIntro);
如果您在调试器中注意到,
console.log("skipIntro is: " + skipIntro);
at Intro.js:16 runs before the console.log("skipIntro is inside Async: " + skipIntro);
在Intro.js:16处在
console.log("skipIntro is inside Async: " + skipIntro);
之前运行console.log("skipIntro is inside Async: " + skipIntro);
at Intro.js:14. 在Intro.js:14。 I am unsure at how to pause the execution of the code until the function returns the appropriate values.
我不确定如何在函数返回适当的值之前暂停执行代码。
I expect the log to output: 我希望日志输出:
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
To pause until the response comes you need to shift your code to inside the callback method or the .then
method inside a promise. 要暂停直到响应到来,您需要将代码移至promise方法内的回调方法或
.then
方法内。
Change this ... 改变这个...
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(); } }
To this ... 为此...
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(); } }); }
Cause if you noticed. 原因,如果您注意到。
This code never runs! 此代码永远不会运行!
if(skipIntro){ console.log("skipping Intro"); Actions.login(); }
Because the skipIntro is false when it was ran. 因为skipIntro在运行时为false。
Simple concept is that JavaScript ran everything else when it was still waiting for a promise to be fulfilled. 简单的概念是,JavaScript在仍在等待兑现诺言的同时运行其他所有内容。
Then it went back to the .then
method when a promise response came. 然后
.then
当诺言响应到来时.then
它又回到.then
方法。
So if you want to wait for a reponse to navigate/call some other function you do it inside the .then
function or callback function. 因此,如果您想等待响应来导航/调用其他函数,请在
.then
函数或回调函数中执行此操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.