The Async Await Event Loop
Languages like JavaScript run user code on a single thread but stay responsive through an event loop. The loop repeatedly takes one ready task, runs it to completion, then picks the next.
When code reaches an await, the function pauses and returns control to the loop instead of blocking. The pending operation, such as a network read, runs in the background. When it finishes, a callback is queued so the suspended function can resume.
Key pieces:
- Call stack runs the current synchronous work.
- Task queue holds callbacks ready to run.
- Microtask queue holds resolved promise continuations, which run before the next task.
Because the thread is never blocked waiting, thousands of slow operations can be in flight at once. The danger is blocking the loop with heavy synchronous computation, which freezes everything.
Async await is concurrency without parallelism: one thread juggles many waiting tasks efficiently.
Key idea
The event loop keeps a single thread busy by suspending awaited work and resuming it through queued callbacks instead of blocking.