Skip to content

Instantly share code, notes, and snippets.

@arackaf
Last active November 8, 2017 10:24
Show Gist options
  • Save arackaf/7826ff661db492c7b5d3ef95dd2afef4 to your computer and use it in GitHub Desktop.
Save arackaf/7826ff661db492c7b5d3ef95dd2afef4 to your computer and use it in GitHub Desktop.
async function a(){
console.log("a");
b();
console.log("b done");
}
async function b(){
console.log("starting b");
await Promise.resolve(res => setTimeout(() => res()), 1000);
console.log("finishing b");
}
a();
/*
logs:
a
starting b
b done
finishing b
since the call to b() above should be `await b();`, even though b awaits the promise inside of it, a will not
await b's completion unless you tell it to. It's crucial to understand that b is literally returning a promise, and that
await b();
is the equivalent of
Promise.resolve(b()).then(result => {
//rest of your function
})
while b() is just, well, b(), where the promise created in `b` is fired but never waited on.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment