Skip to content

Async

The event loop, drawn

The call stack, the microtask queue and the macrotask queue, and the order they actually run in.

12 minutes · call stack · microtask queue · macrotask queue

Three places code can be waiting

There is the stack — the code running right now, which runs to completion and is not interrupted. There is the microtask queue, where promise callbacks wait. And there is the macrotask queue, where timers and I/O callbacks wait.

The rule is short and almost nobody can state it cold: run the current stack to the end, then drain the *entire* microtask queue, then take one macrotask, then drain the microtask queue again. Microtasks queued by microtasks join the same drain. A timer set for zero milliseconds does not run before a promise callback queued after it.

Everything you do here stays in this browser.