Back to results for “OOP”
JavaScript › Event Loop & Concurrency

How does the event loop decide what to run next?

Intermediate

Answer

After each synchronous script (or a single macrotask) finishes, the event loop first fully drains the microtask queue, then renders any pending UI updates, then picks up the next macrotask from the queue — repeating this cycle continuously.
// Simplified loop:
// while (true) {
//   runOneMacrotask();
//   drainAllMicrotasks();
//   renderIfNeeded();
// }
Real-world example Explaining why UI updates can appear to 'batch' after several synchronous state changes but before the next repaint.

Common follow-ups: Where do requestAnimationFrame callbacks fit into this cycle?