Question Bank

Event Loop Trace Challenge

Difficulty: Medium

Mid-tier traces of the JavaScript event loop covering microtasks, macrotasks, and the interleaving of `setTimeout`, `Promise.then`, and `queueMicrotask`. Predict every line.

Question Bank
/

Event Loop Trace Challenge

Event Loop Trace Challenge

Mid-tier traces of the JavaScript event loop covering microtasks, macrotasks, and the interleaving of `setTimeout`, `Promise.then`, and `queueMicrotask`. Predict every line.

Question Bank
Medium
JavaScript
Python
5 questions
js-event-loop
js-microtask-macrotask
async-programming
interview-prep

657 views

10

Trace the output line by line. State which entries are microtasks and which are macrotasks.

Examples

Example 1:

Input: console.log('1'); setTimeout(() => console.log('2'), 0); Promise.resolve().then(() => console.log('3')); console.log('4');
Output: 1, 4, 3, 2
Explanation: Synchronous statements run first (1 then 4). When the task ends, the runtime drains the microtask queue (3) before picking the next macrotask (2). This 'microtasks before macrotasks' rule is the central event-loop invariant.