【问题标题】:Pause execution of code until Asynchronous function returns the desired value暂停执行代码,直到异步函数返回所需的值
【发布时间】: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


    【解决方案1】:

    要在响应到来之前暂停,您需要将代码移至回调方法或 Promise 中的 .then 方法。

    改变这个...

    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();
        }
    }

    到这个...

    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();
                }
        });
    }

    如果你注意到了。

    这段代码永远不会运行!

        if(skipIntro){
            console.log("skipping Intro");
            Actions.login();
        }

    因为skipIntro在运行时是假的。

    简单的概念是,当 JavaScript 仍在等待承诺被履行时,它会运行其他所有内容。

    然后当一个promise响应来的时候,它又回到了.then方法。

    因此,如果您想等待响应导航/调用其他函数,请在 .then 函数或回调函数中进行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多