The Event Loop
Understand how single-threaded JavaScript handles asynchronous tasks using the Call Stack, Web APIs, and the Microtask Queue.
The Single-Threaded Illusion
JavaScript is strictly single-threaded, meaning it can only execute one piece of code at a time on its main thread.
To prevent the browser from freezing during slow tasks (like fetching data), JavaScript offloads work to the browser's background APIs, orchestrating the results via the Event Loop.
The Call Stack (LIFO)
The **Call Stack** is where your synchronous code actually runs. It operates on a 'Last In, First Out' (LIFO) principle.
If function A calls function B, A is paused and B is placed on top of the stack. When B finishes, it pops off, and A resumes.
If the Call Stack is busy, *nothing else can happen*. The browser cannot even render pixels until the stack is empty.
The Queues: Micro vs Macro
When a background task finishes, its callback isn't executed immediately. It is pushed into a waiting line. There are two distinct lines:
1. **The Microtask Queue**: Reserved for high-priority tasks, primarily resolved Promises (.then).
2. **The Macrotask Queue**: Reserved for standard callbacks like setTimeout, setInterval, and DOM events.
The Event Loop's only job is to watch the Call Stack. If the Stack is empty, it drains the *entire* Microtask queue first. Only when the Microtask queue is empty will it allow a Macrotask to run.
The Loop Architecture
Examine the architecture of the Event Loop to visualize how synchronous code, Web APIs, and priority queues interact.
The Loop Architecture
console.log("Start");
setTimeout(() => {
console.log("Timeout");
}, 0);
Promise.resolve().then(() => {
console.log("Promise");
});
If empty, pulls from queues.
2. Click events
Check yourself
Pick an answer to lock it in, then read why. Getting one wrong is part of how it sticks.
Remember this
- JavaScript is single-threaded and executes synchronously on the Call Stack.
- Background work (timers, network requests) is offloaded to the browser's Web APIs.
- Resolved Promises go to the high-priority Microtask Queue.
setTimeoutand DOM events go to the lower-priority Macrotask Queue.- The Event Loop only pulls from the queues when the Call Stack is completely empty.
- The Event Loop always drains the Microtask Queue entirely before running a Macrotask.
Done with this concept?
Mark it complete to track your progress. No login needed.