Study notes · 10% of the exam

The Event Loop

One main thread runs tasks one at a time; microtasks run between them. That explains most ordering puzzles.

Key points

  1. 1

    The current script (a task) always runs to completion before anything else.

  2. 2

    After each task, the microtask queue (promise callbacks, queueMicrotask) is drained completely.

  3. 3

    Then the next task runs: a timer, an event, a message. Rendering happens between tasks.

  4. 4

    Long synchronous work blocks input and rendering; split it up or move it to a Web Worker.

Common traps

  • setTimeout(fn, 0) does not run immediately. It waits for the current task and all microtasks.

Test yourself on The Event Loop

Ten questions, with the answer and explanation after each one.