JavaScriptintermediate30 minFree
The event loop
Call stack, task queues and microtasks — why your code ran in that order.
In short
JavaScript runs on one thread with one call stack. Anything asynchronous is handed to the host environment, which puts a callback in a queue when it is ready. The event loop moves work from those queues onto the stack, but only when the stack is empty. Microtasks — promise continuations — drain completely between each task, which is why an await resolves before a setTimeout of zero. Almost every ordering surprise in JavaScript follows from those rules.
Contents
Downloads
Event loop reference card PDF · 95 KB
- #async
- #promises
- #runtime
- #concurrency