Blocking the loop β€” interactive event loop example

1setTimeout(() => {
2 console.log('am I late?');
3}, 0);
4
5console.log('start spinning');
6
7while (true) {
8 // spinning forever…
9}

A 0ms setTimeout is registered. The callback will be ready in the task queue almost immediately.

1 / 6

Call stack

script

Web APIs β€” browser land

timeout cb 0ms

The event loop β€” is the call stack empty? β†’ drain the microtask queue first β†’ then take one callback.

Microtask queue β€” priority

Callback queue

Console

Fig. β€” Blocking the loop Β· step 1 of 6

← β†’ step Β· space play Β· 123 level