简体   繁体   English

等待后未执行异步/等待代码

[英]Async/Await code not being executes after await

I have been going over async/await.我一直在经历异步/等待。 I trying few simple examples but unable to understand flow of async and await .我尝试了一些简单的示例,但无法理解 async 和 await 的流程。 In below code在下面的代码中

 function wait(ms) { return new Promise(r => setTimeout(function() { console.log('Hello'); }, ms)); } async function GetUser() { await wait(5000); console.log('world'); } GetUser();

Why is the message "world" not logged?为什么没有记录“世界”消息? Only "Hello" prints.只打印“你好”。

You need to resolve it. 您需要解决它。 So call r() 所以叫r()

 function wait(ms) { return new Promise(r => setTimeout(function() { console.log('Hello'); r() }, ms)); } async function GetUser() { await wait(3000) console.log('world'); } GetUser() 

You should call the resolver. 您应该致电解析器。

function wait(ms) { 
 return new Promise(r => setTimeout(function(){console.log('Hello'); r();}, 
//                                                                   ^^^ this
ms));
}

Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise 参考: https : //developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Promise

  • Here wait() method returns a Promise, the function of await keyword is to stop code execution until the returned Promise by wait() is resolved.这里wait()方法返回一个Promise, await关键字的作用是停止代码执行,直到wait()返回的Promise被解决。

  • So the code snippet in question, without using async/await will be as below.所以有问题的代码片段,不使用async/await将如下所示。 Note that the resolve function is not yet invoked here.请注意,此处尚未调用 resolve 函数。 Hence, once the snippet is executed only Hello will get printed in the console.因此,一旦代码片段被执行,只有Hello会被打印在控制台中。

     // First code snippet function wait(ms) { return new Promise(r => setTimeout(function () { console.log('Hello'); }, ms)); } function GetUser() { return wait(1000) .then(() => { console.log("World") }) .catch((err) => { console.log(err); }) } GetUser()

  • Code snippet when r() is invoked.调用r()时的代码片段。 Here, once the snippet is executed Hello and World both are printed in the console.在这里,一旦代码片段被执行, HelloWorld都会打印在控制台中。

     // Second code snippet function wait(ms) { return new Promise(r => setTimeout(function () { console.log('Hello'); r(); }, ms)); } function GetUser() { return wait(1000) .then(() => { console.log("World") }) .catch((err) => { console.log(err); }) } GetUser()

  • The reason why the second code snippet works, has to do with how Promise is implemented in JS.第二个代码片段起作用的原因与 Promise 在 JS 中的实现方式有关。

    • Handler function/functions attached to a promise with the help of .then() are invoked only when the promise is resolved.仅当 promise 被解决时,才会调用在.then()的帮助下附加到 promise 的处理函数/函数。

    • Code snippet when resolve method is not invoked inside the executor function ie, when the Promise is not resolved.执行器函数内部未调用resolve方法时的代码片段,即当Promise未解析时。 Here, only code inside setTimeout is invoked and not the handlers.在这里,只有setTimeout内的代码被调用,而不是处理程序。

       function wait(ms) { return new Promise(r => setTimeout(function () { console.log('Hello'); }, ms)); } const promiseReturnedByWaitMethod = wait(2000); // Multiple handlers can be added to a promise reference, which are executed when the asynchronous operation is completed. // adding first handler promiseReturnedByWaitMethod.then(() => { console.log("First handler!!") }); // adding second handler promiseReturnedByWaitMethod.then(() => { console.log("Second handler!!") });

    • Code snippet when resolve method is invoked inside the executor function ie, when the Promise is resolved.在 executor 函数内部调用resolve方法时的代码片段,即当Promise被解析时。

       function wait(ms) { return new Promise(r => setTimeout(function () { console.log('Hello'); r(); }, ms)); } const promiseReturnedByWaitMethod = wait(2000); // Multiple handlers can be added to a promise reference, which are executed when the asynchronous operation is completed. // adding first handler promiseReturnedByWaitMethod.then(() => { console.log("First handler!!") }); // adding second handler promiseReturnedByWaitMethod.then(() => { console.log("Second handler!!") });

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

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