Skip to content

The event loop

Why the log printed last

The synchronous run finishes first, and all of it. Then two queues, in order. · 12 minutes

Nothing you queue runs while there is still synchronous work to do. Not a promise that already resolved, not a timer with a zero delay. The current run finishes, entirely, and only then does anything else get a turn.

Then the microtask queue drains — all of it, including anything queued while it was draining — and only after that does the earliest timer fire.

`setTimeout(f, 0)` then `Promise.resolve().then(g)` then `console.log('last line')` — what order?