🟑 MediumEvent Loop & PromisesπŸ’» Output Question

Classic setTimeout vs Promise

πŸ’‘

Hint

Microtasks (Promises) always run before macrotasks (setTimeout), even with 0ms delay.

What does this output?

console.log('1');

setTimeout(() => console.log('2'), 0);

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

console.log('4');

Correct Output

1
4
3
2

Why this output?

Explanation: Sync code runs first (1, 4). Then microtasks drain (Promise β†’ 3). Then macrotask queue (setTimeout β†’ 2).

Key Insight: Microtasks (Promises) always run before macrotasks (setTimeout), even with 0ms delay.

More Event Loop & Promises Output Questions

πŸ”΄ HardNested setTimeout and Promiseβ†’πŸŸ‘ Mediumasync/await execution orderβ†’πŸ”΄ HardPromise chaining orderβ†’πŸŸ‘ MediumPromise constructor is synchronousβ†’

Practice predicting output live β†’

66 output questions with instant feedback

πŸ’» Try Output Quiz