setTimeout(0), alone โ€” interactive event loop example

1console.log('before');
2
3setTimeout(() => {
4 console.log('later');
5}, 0);
6
7console.log('after');

The script starts on the call stack. console.log("before") runs synchronously.

1 / 7

Call stack

script

Web APIs โ€” browser land

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

Microtask queue โ€” priority

Callback queue

Console

01before

Fig. โ€” setTimeout(0), alone ยท step 1 of 7

โ† โ†’ step ยท space play ยท 123 level