My understanding is that the promise in await promise
expression gets pushed to Microtask Queue, so it would settle after the call stack is empty. The below code works as I expected:
async function func() {
console.log(await Promise.resolve('bagel'));
}
func();
console.log('synchronous log');
// Result:
// synchronous log
// bagel
But if I await
something not a Promise like this, the order of the logs changed:
async function func() {
await console.log('bagel');
}
func();
console.log('synchronous log');
// Result:
// bagel
// synchronous log
What happens behind-the-scenes when I call await
? What is the order of executing when awaiting a non-Promise? And specifically await console.log
?