Event Loop, Macrotasks, and Microtask Queue
Prevent 60fps frame starvation and Promise-loop freezes—the Event Loop drains the entire Microtask Queue after every single macrotask before painting.
About this page
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.
Where this connects
Prerequisites
Browser Rendering Pipeline (Critical Rendering Path)
Update the Rendering runs only after the Microtask Queue is completely drained.
Next & related
Async Determinism, Fake Timers & Microtask Draining
Advancing fake macrotask timers without draining the microtask queue leaves Promise continuations unresolved.
BroadcastChannel and Web Locks API
BroadcastChannel messages and Web Lock grants arrive as asynchronous tasks on each context's event loop.
Core Web Vitals Mechanics (LCP, INP, CLS)
INP input delay occurs when a long main-thread task blocks event dispatch.
Debounce, Throttle & rAF Coalescing
Debounce/throttle timers schedule macrotasks while requestAnimationFrame hooks into the render step.
Dedicated Web Workers and Transferables
Dedicated workers run an independent event loop with their own task and microtask queues.
Signals and Fine-Grained Reactivity (Reactive Graph)
Reactive effect batching commonly relies on a microtask flush.
IndexedDB Transactions and Storage Quotas
IDB transaction auto-commit is tied directly to microtask queue exhaustion.
Long Tasks and Interaction to Next Paint (INP)
Input events cannot run until the currently executing main-thread macrotask finishes.
MutationObserver and Batched Microtask Delivery
MutationObserver callbacks execute during the Microtask Checkpoint before Update the Rendering.
Promises and async/await Internals
Promise fulfillment reactions always enqueue onto the Microtask Queue rather than the Task Queue.
requestAnimationFrame & VSync
rAF runs in the rendering step of the HTML event loop after tasks and microtasks.
Prioritized Task Scheduling and scheduler.yield()
scheduler.yield() exits the current macrotask so the Event Loop can process input and paint.
Streams API and Backpressure Mechanics
Stream pull(), write(), and pipeTo() promises settle via microtasks between chunk transfers.
V8 Hidden Classes (Shapes) and Inline Caches
Hot code executed inside JS tasks is optimized by Ignition and TurboFan.
Web Crypto API and SubtleCrypto
SubtleCrypto returns Promises that resolve on the microtask queue after native thread work.
Mechanical Comparisons
- Contrasts WithDebounce, Throttle & rAF Coalescing vs Event Loop, Macrotasks, and Microtask Queue
Raw tasks and microtasks fire per event, whereas rAF coalescing batches callbacks once per vsync frame.