How does async/await map onto the microtask queue under the hood?
Short answer
An async function returns immediately at its first await, suspending execution; the code after await is scheduled to resume as a microtask once the awaited Promise settles — functionally equivalent to chaining .then() callbacks, just with more readable syntax.
Advertisement
AModel answer
An async function returns immediately at its first await, suspending execution; the code after await is scheduled to resume as a microtask once the awaited Promise settles — functionally equivalent to chaining .then() callbacks, just with more readable syntax.
async function run() {
console.log('1');
await null; // yields, resumes as a microtask
console.log('2');
}
run();
console.log('3');
// logs: 1, 3, 2Real-world example
Understanding why code immediately after an awaited call doesn't run synchronously, even for an already-resolved value.
Likely follow-ups
- Does 'await null' behave differently from 'await Promise.resolve()'?
Related topics