πŸ”΄ HardEvent Loop & PromisesπŸ’» Output Question

queueMicrotask vs Promise

πŸ’‘

Hint

queueMicrotask and Promise.resolve().then() are equivalent in priority β€” FIFO within the microtask queue.

What does this output?

console.log('1');

queueMicrotask(() => console.log('2'));

Promise.resolve().then(() => console.log('3'));

queueMicrotask(() => console.log('4'));

console.log('5');

Correct Output

1
5
2
3
4

Why this output?

Explanation: queueMicrotask and Promise.then both add to the microtask queue in order. 1 and 5 run sync. Then microtasks drain in order: 2, 3, 4.

Key Insight: queueMicrotask and Promise.resolve().then() are equivalent in priority β€” FIFO within the microtask queue.

More Event Loop & Promises Output Questions

🟑 MediumClassic setTimeout vs Promiseβ†’πŸ”΄ HardNested setTimeout and Promiseβ†’πŸŸ‘ Mediumasync/await execution orderβ†’πŸ”΄ HardPromise chaining orderβ†’

Practice predicting output live β†’

66 output questions with instant feedback

πŸ’» Try Output Quiz