The Event Loop, Visualized

The examples

In curriculum order — each one asks for your prediction first, then animates exactly why. Warm-ups build the model, core examples are the interview classics, advanced ones break the loop on purpose — including Node, where the same language runs on a different loop.

  1. Fig. 1warm-up

    One thing at a time

    No async at all — how the stack really runs your code.

    7 steps · predict first →

  2. Fig. 2warm-up

    setTimeout(0), alone

    Zero milliseconds does not mean now.

    7 steps · predict first →

  3. Fig. 3warm-up

    A promise, alone

    Even an already-resolved promise waits its turn.

    6 steps · predict first →

  4. Fig. 4core

    Promise vs setTimeout

    The classic showdown — and the classic interview question.

    9 steps · predict first →

  5. Fig. 5core

    .then after .then

    Each link in a .then chain is its own microtask.

    7 steps · predict first →

  6. Fig. 6core

    setTimeout inside a for loop

    Why it logs 3, 3, 3 — a closure bug and a timing bug at once.

    11 steps · predict first →

  7. Fig. 7core

    async/await, unmasked

    Watch a function leave the stack mid-body.

    7 steps · predict first →

  8. Fig. 8core

    Promise.all — waiting in parallel

    Two waits, one wait's worth of time — if you start them first.

    9 steps · predict first →

  9. Fig. 9core

    fetch — a real network wait

    A real network round trip, and the thread never waits.

    9 steps · predict first →

  10. Fig. 10core

    Clicks are just tasks

    Your click handler queues behind everything else.

    8 steps · predict first →

  11. Fig. 11advanced

    The checkpoint runs between every task

    Microtasks drain after every task — not once at the end.

    10 steps · predict first →

  12. Fig. 12advanced

    Starving the loop

    VIPs forever — the timer never eats.

    8 steps · predict first →

  13. Fig. 13advanced

    requestAnimationFrame vs setTimeout

    The render step: neither a task nor a microtask.

    10 steps · predict first →

  14. Fig. 14advanced

    Blocking the loop

    One hot loop freezes the whole page.

    6 steps · predict first →

  15. Fig. 15advanced

    Node: nextTick, promises, timers, immediate

    Same language, different loop: libuv phases and nextTick.

    11 steps · predict first →