The JavaScript event loop
How JavaScript runs asynchronous code on a single thread: the call stack, web APIs, the task queue and the microtask queue, why promises run before setTimeout, a step-by-step trace of a classic output puzzle, async and await, rendering, and blocking the loop in browsers and Node.js.
CodexBash, , 4 min read
JavaScript runs your code on a single thread, yet a web page can fetch data, run timers and respond to clicks all at once without freezing. The mechanism that makes this possible is the event loop. Understanding it explains why a setTimeout of zero milliseconds does not run immediately, why promises resolve before timers, why a long loop freezes the whole page, and how to answer one of the most popular JavaScript interview questions. This article builds the model piece by piece.
Sign in to read the rest
The whole 4 minute article is free with an account, along with every problem, lesson and tutorial. No card.