简体   繁体   English

暂停执行代码,直到异步函数返回所需值

[英]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.

相关问题 暂停函数执行,直到另一个函数返回 - Pause function execution until another function returns javascript暂停执行功能,直到触发事件 - javascript pause function execution until event is triggered 如何推迟执行代码,直到获取返回值 - How do I defer the execution of code until fetch returns value Javascript:map 异步 function 在数组上,在每个元素上保持执行,直到它返回前一个元素 - Javascript: map asynchronous function over array, holding execution on each element until it returns on the previous one 我可以暂停执行功能,直到在节点中收到一些值吗? - Can I pause the execution of function until some value has been received in node? JavaScript /异步函数调用是否有用于阻止直到函数返回的简写? - JavaScript/Asynchronous Function Calls Is there a Shorthand for blocking until function returns? 如何在调用异步函数之后的javascript / jquery代码中创建暂停 - How to create a pause in javascript/jquery code that follows a call to asynchronous function 取决于JavaScript中的异步功能,暂停整个脚本执行的正确方法是什么? - What's the correct way to pause the entire script execution contingent on an asynchronous function in JavaScript? 在Node.js中执行异步代码 - Execution of asynchronous code in nodejs 异步代码执行 - Asynchronous code execution
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM