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

requestAnimationFrame vs setTimeout order

πŸ’‘

Hint

rAF fires in the render phase of the event loop β€” before paint but after microtasks. setTimeout(0) fires as a macrotask after the render phase.

What does this output?

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

requestAnimationFrame(() => console.log('rAF'));

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

console.log('sync');

Correct Output

sync
microtask
rAF
timeout

Why this output?

Explanation: "sync" first. Microtask drains: "microtask". Then browser render phase fires: rAF runs just before paint. Then macrotask: "timeout". (Note: exact rAF vs timeout order can vary by browser implementation, but rAF typically precedes setTimeout in the same frame.)

Key Insight: rAF fires in the render phase of the event loop β€” before paint but after microtasks. setTimeout(0) fires as a macrotask after the render phase.

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