admin管理员组文章数量:1336367
I have been going over async/await. I trying few simple examples but unable to understand flow of async and 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();
I have been going over async/await. I trying few simple examples but unable to understand flow of async and 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.
Share Improve this question edited Dec 10, 2018 at 15:51 Siva asked Dec 7, 2018 at 15:12 SivaSiva 572 silver badges10 bronze badges 03 Answers
Reset to default 5You should call the resolver.
function wait(ms) {
return new Promise(r => setTimeout(function(){console.log('Hello'); r();},
// ^^^ this
ms));
}
Reference: https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
You need to resolve it. So call 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()
Here
wait()
method returns a Promise, the function ofawait
keyword is to stop code execution until the returned Promise bywait()
is resolved.So the code snippet in question, without using
async/await
will be as below. Note that the resolve function is not yet invoked here. Hence, once the snippet is executed onlyHello
will get printed in the console.// 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. Here, once the snippet is executedHello
andWorld
both are printed in the console.// 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.
Handler function/functions attached to a promise with the help of
.then()
are invoked only when the promise is resolved.Code snippet when
resolve
method is not invoked inside the executor function i.e, when the Promise is not resolved. Here, only code insidesetTimeout
is invoked and not the handlers.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 pleted. // 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 i.e, when the Promise is resolved.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 pleted. // adding first handler promiseReturnedByWaitMethod.then(() => { console.log("First handler!!") }); // adding second handler promiseReturnedByWaitMethod.then(() => { console.log("Second handler!!") });
本文标签: javascriptAsyncAwait code not being executes after awaitStack Overflow
版权声明:本文标题:javascript - AsyncAwait code not being executes after await - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742390099a2465817.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论