GoalUnderstand the Event Loop deeply enough to predict any async output order, explain UI freezes, and know where rendering fits.
Lessons
The Event Loop — four pieces
JavaScript is single-threaded — one Call Stack, one thing at a time. But the browser has other threads for networking, timers, and I/O. The Event Loop coordinates between them.
Four pieces:
1. Call Stack — where your JS runs. One frame per function call.
2. Web APIs — browser features (setTimeout, fetch, DOM events) that run on other threads.
3. Microtask Queue — high-priority callbacks: Promise.then, await continuations, queueMicrotask.
4. Macrotask Queue — lower priority: setTimeout, setInterval, DOM events.
The Loop's rule:
> After each task (and after initial script), drain ALL microtasks before taking the next macrotask. Render may happen between macrotasks.
setTimeout(fn, 0) does NOT mean "run now". It means "queue in the macrotask queue after at least 0ms" — always behind all sync code and microtasks.
Blocking the main thread
The Event Loop only moves a callback when the Call Stack is empty. If synchronous code keeps the stack busy, nothing else can run — no callbacks, no paint, no user input.
This is the root cause of INP (Interaction to Next Paint) being terrible. Fix: Web Workers (offload to another thread) or chunking (break work into pieces, yielding between them with setTimeout).
Rendering Pipeline
The browser renders at ~60fps (every ~16.6ms). Rendering shares the main thread with JS.