The event loop in one screen

The one-page summary of the lesson The event loop. It states the answers, so if you want them to stick, play the lesson first: it makes you predict before it explains.

The points

  • Synchronous code runs to completion on the call stack.
  • Web APIs (timers, fetch, events) run outside JS and queue callbacks.
  • Promise callbacks and queueMicrotask go to the microtask queue.
  • Timers and events go to the task (macrotask) queue.
  • After each task: drain all microtasks, then the browser may render, then the next task.
  • setTimeout(fn, 0) means 'as soon as possible after the current task', not 'now'.

What catches people out

  • Recursive microtasks freeze the page
  • Out-of-order fetch responses overwrite fresh state

The wrong idea, and the right one

Often believed: setTimeout(fn, 0) runs fn immediately, before anything else that's queued.

What really happens: Callbacks wait in queues. After the current script finishes, every microtask runs before the next task (such as a timer callback).

Step by step

  1. Symptom. Out-of-order results usually mean out-of-order callbacks.
  2. Sync code. Synchronous code runs to completion first.
  3. Queues. Two queues, two priorities.
  4. Microtasks drain. Microtasks drain completely before the next task and before rendering.
  5. Break it. Endless microtasks block timers and paint.
  6. Agent bug. Callbacks run in arrival order, so a stale response can win.
  7. Recap. Stack → all microtasks → one task → repeat.

Sources

Play the lesson: The event loop