简体   繁体   English

异步/等待在我的示例中不起作用

[英]Async/Await doesn't work in my example

Many JavaScript developers know about async/await and benefits of them so I try to test a old example of asynchronous action, let's see my experiment: 许多JavaScript开发人员都知道async/await及其好处,因此我尝试测试一个异步操作的旧示例,让我们看一下我的实验:

Undoubtedly the answer of below code is: 无疑,以下代码的答案是:

/*Line: 1*/ console.log(`1`);
/*Line: 2*/ console.log(`2`);
/*Line: 3*/ console.log(`3`);

//==>  123

So I wanna put a setTimeout for second line: 所以我想在第二行输入setTimeout

/*Line: 1*/ console.log(`1`);
/*Line: 2*/ setTimeout(() => {
               console.log(`2`);
            }, 0);
/*Line: 3*/ console.log(`3`);

//==> 132

Because of asynchronous action of second line the 2 is after the 13 , so in the console 132 is showed. 由于第二行的异步作用,因此213 ,因此在控制台132中显示。

I decide to use async/await feature of new JavaScript to see 123 again, so I wrote the above code like this: 我决定使用新JavaScript async/await功能再次查看123 ,因此我编写了上面的代码,如下所示:

(async () => {
    console.log(`1`);
    await setTimeout(() => {
        console.log(`2`);
    }, 0);
    console.log(`3`);
})();

But it doesn't work and I saw 132 again in my console. 但这不起作用,我再次在控制台中看到132 Which part I did wrong or I don't know about it? 我哪部分做错了或不知道?

await waits for promise to be resolved. await等待诺言解决。 Since setTimeout is not a promise, execution of program will not wait till it is executed. 由于setTimeout不是一个承诺,因此程序的执行不会等到执行完毕。 You would need to wrap setTimeout() within promise as specified in first example of following link for your example to work as expected: 您需要按照以下链接的第一个示例中的指定将setTimeout()包装在promise中,以使示例按预期工作:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Statements/async_function

setTimeout is not returning a promise. setTimeout没有返回承诺。 In order to get it to work, you will have to create a function returning a promise that is resolved inside the setTimeout callback. 为了使其正常工作,您将必须创建一个函数,该函数返回在setTimeout回调内部解析的Promise。

 (async () => { console.log(`1`); await new Promise(resolve => { setTimeout(() => { resolve('resolved'); console.log(`2`); },0); }); console.log(`3`); })(); 

You can only await a function which returns a promise. 您只能等待返回承诺的函数。 Note that await can only be used inside an async function: 请注意,await只能在异步函数内使用:

 async function someFunc() { console.log(`1`); await new Promise((resolve, reject) => setTimeout(() => { console.log(`2`); resolve(); }, 0)); console.log(`3`); } someFunc(); 

Await should be called on a function that returns a promise (otherwise it just returns). 应该在返回promise的函数上调用Await(否则它将返回)。

setTimeout does not return a promise so you should wrap it with a function that returns a promise. setTimeout不返回承诺,因此应使用返回承诺的函数将其包装。

Read more about it here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await 在此处阅读有关此内容的更多信息: https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await

This code should do the work. 此代码应能完成工作。

 (async() => { console.log(`1`); await (() => { return new Promise( resolve => { setTimeout(() => { console.log(2); resolve() }, 1000); }); })(); console.log(`3`); })(); 

您需要创建函数,例如async function foo() {} ,并在其中进行await调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM