Event Loop, Macrotasks, and Microtask Queue
The Event Loop dequeues one macrotask, runs the Call Stack until empty, then drains every microtask (Promises, queueMicrotask, MutationObserver) before the engine can paint the next frame.
Task vs Microtask vs Frame Scheduling
Observe why Promise.then always runs before setTimeout(0) and before Paint.
Synchronous script schedules setTimeout(cb, 0) then Promise.resolve().then(micro).
setTimeout(cb, 0);
Promise.resolve().then(micro);- callStack
- [ "script()" ]
- microtaskQueue
- [ "micro" ]
- taskQueue
- [ "cb" ]
Under the Hood
The browser Event Loop dequeues one macrotask, runs the Call Stack to completion, and drains the entire Microtask Queue (including microtasks queued during the drain) before entering Update the Rendering. Like a tollbooth with an express VIP lane, the next frame waits until the microtask lane is empty.
- Task (Macrotask): An event (
click,setTimeout, initial script) enters the Call Stack and executes run-to-completion without preemption. - Microtask Checkpoint: Once the Call Stack empties, V8 drains every queued microtask, including new microtasks scheduled during the drain.
- Update the Rendering: When a frame is due (16.6 ms at 60 Hz), the engine runs
requestAnimationFramecallbacks, then Style, Layout, Paint, and Composite in the Browser Rendering Pipeline.
setTimeout(() => console.log('4: macrotask'), 0);
requestAnimationFrame(() => console.log('3: before paint'));
Promise.resolve().then(() => console.log('2: microtask'));
console.log('1: synchronous call stack');Problem Genealogy
- 1995 (Single-threaded JS): Synchronous execution model driven by timer and DOM event callbacks.
- 2015 (ES6 Promises): Dedicated priority queue (Microtask Queue) introduced to settle promises before the next render cycle.
- 2024+ (
scheduler.yield()): Explicit API to yield the main thread while retaining higher priority than third-party tasks, fixing the oldsetTimeout(fn, 0)hack that sent your continuation to the back of the queue.
Production Pitfalls
- Render Starvation: A recursive
Promise.resolve().then(...)loop prevents the Event Loop from ever reaching the render phase, freezing the tab completely. - rAF vs Promise Ordering: Scheduling a DOM read inside
.then()runs beforerequestAnimationFrame(see Debounce, Throttle, and rAF Coalescing) and can trigger Layout Thrashing on unsynchronized styles.
- When not to use
queueMicrotask: Never chunk heavy CPU work with microtasks (they still block the frame); usescheduler.yield()or a Web Worker.
Connections & Logical Next Steps
- What This UnlocksAsync Determinism, Fake Timers & Microtask Draining
- What This UnlocksBroadcastChannel and Web Locks API
- What This UnlocksCore Web Vitals Mechanics (LCP, INP, CLS)
- What This UnlocksDebounce, Throttle & rAF Coalescing
- What This UnlocksDedicated Web Workers and Transferables
- What This UnlocksSignals and Fine-Grained Reactivity (Reactive Graph)
- What This UnlocksIndexedDB Transactions and Storage Quotas
- What This UnlocksLong Tasks and Interaction to Next Paint (INP)
- What This UnlocksMutationObserver and Batched Microtask Delivery
- What This UnlocksPromises and async/await Internals
- What This UnlocksrequestAnimationFrame & VSync
- What This UnlocksPrioritized Task Scheduling and scheduler.yield()
- What This UnlocksStreams API and Backpressure Mechanics
- What This UnlocksV8 Hidden Classes (Shapes) and Inline Caches
- What This UnlocksWeb Crypto API and SubtleCrypto
- Under the HoodBrowser Rendering Pipeline (Critical Rendering Path)
Advancing fake macrotask timers without draining the microtask queue leaves Promise continuations unresolved.
BroadcastChannel messages and Web Lock grants arrive as asynchronous tasks on each context's event loop.
INP input delay occurs when a long main-thread task blocks event dispatch.
Debounce/throttle timers schedule macrotasks while requestAnimationFrame hooks into the render step.
Dedicated workers run an independent event loop with their own task and microtask queues.
Reactive effect batching commonly relies on a microtask flush.
IDB transaction auto-commit is tied directly to microtask queue exhaustion.
Input events cannot run until the currently executing main-thread macrotask finishes.
MutationObserver callbacks execute during the Microtask Checkpoint before Update the Rendering.
Promise fulfillment reactions always enqueue onto the Microtask Queue rather than the Task Queue.
rAF runs in the rendering step of the HTML event loop after tasks and microtasks.
scheduler.yield() exits the current macrotask so the Event Loop can process input and paint.
Stream pull(), write(), and pipeTo() promises settle via microtasks between chunk transfers.
Hot code executed inside JS tasks is optimized by Ignition and TurboFan.
SubtleCrypto returns Promises that resolve on the microtask queue after native thread work.
Update the Rendering runs only after the Microtask Queue is completely drained.
While cached in bfcache, JS tasks and timers are frozen and resumed upon restoration.
Each user interaction event is dispatched as a macrotask with a microtask checkpoint after every listener.
The Event Loop dequeues a new task only after the Call Stack completely unwinds to empty.
V8 schedules incremental marking steps and idle-time GC work between Event Loop tasks.
Service workers execute in an isolated worker event loop and wake only for functional events.
Deferred timers and promises execute long after the render call stack that created their closure popped.
Raw tasks and microtasks fire per event, whereas rAF coalescing batches callbacks once per vsync frame.