Skip to content
JavaScriptintermediate30 minFree

The event loop

Call stack, task queues and microtasks — why your code ran in that order.

Share

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

  1. 01The call stackOne thread, one stack, and what "blocking" actually blocks.
  2. 02Tasks and microtasksTwo queues, different draining rules, and the ordering that follows.

Downloads

Event loop reference card PDF · 95 KB
  • #async
  • #promises
  • #runtime
  • #concurrency