The Heartbeat of JS: Understanding the Event Loop (2026)
By Huzi
File #450 · 4 min read
JavaScript is single-threaded, yet it fetches data, runs timers, and streams chat responses without freezing the page. Every junior developer is told this; few are told how. The answer is the event loop, and once you can see it, half of async JavaScript stops being superstition.
The quick answer: Your code runs on one call stack; everything slow — timers, network, events — is outsourced to Web APIs, and finished callbacks re-enter through queues. Promises use a separate microtask queue that always drains before the next timer, and blocking the stack freezes everything. That is the whole machine.
Where your code actually lives
Three places, constantly handing work to each other:
| Place | What it does | Example |
|---|---|---|
| The call stack | Runs synchronous code, one frame at a time, last in first out — while it is busy, nothing else on the main thread happens | Every function you call |
| The Web APIs | The browser's back kitchen, where timing and networking are taken off your hands | setTimeout, fetch, DOM events |
| The callback queue | The pick-up counter where finished callbacks wait their turn | setTimeout(fn, 0) after the stack empties |
| The microtask queue | Where promises wait — and always get called before the next timer | .then callbacks, await continuations, queueMicrotask |
| libuv in Node.js | Runs the same design, its loop polling for I/O and running ready callbacks on the main thread | Server-side async |
The loop itself asks two questions forever: is the stack empty, and is a callback waiting? Only when both answers align does a task get pushed on. That is why setTimeout(fn, 0) never runs immediately — zero milliseconds is a queue position, not a starting gun.
Microtasks jump the queue
Promises do not wait in the regular line. .then callbacks, await continuations, and queueMicrotask go to the microtask queue, and the loop drains it completely after every macrotask — before the next timer, before rendering. Predict the output before you run it:
console.log("1");
setTimeout(() => console.log("2"), 0);
Promise.resolve().then(() => console.log("3"));
console.log("4");
// Output: 1, 4, 3, 2
Pro tip: Respect the queue — an infinite microtask loop starves rendering, timers and input, and the tab hangs.
Do not block the loop
A long synchronous loop is the classic footgun: the page cannot paint, clicks go dead, and the browser offers to close the tab. Break the work up and hand control back periodically. scheduler.yield(), the successor to scheduler.postTask, is now baseline in Chrome, Edge, and Safari:
async function processItems(items) {
for (const item of items) {
doExpensiveWork(item);
await scheduler.yield(); // let the browser paint and take input
}
}
For genuinely heavy computation, leave the main thread altogether — Web Workers run real parallel threads and message results back home:
const worker = new Worker("./heavy.js", { type: "module" });
worker.postMessage({ data: largeArray });
worker.onmessage = (e) => updateUI(e.data);
And for token streams from LLM endpoints or large file reads, the async iterator rides the same microtask machinery with cleaner syntax:
const response = await fetch("/api/chat");
for await (const chunk of response.body) {
appendToUi(chunk);
}
A good travel desk schedules the way this section preaches: fare searches, visa queues and rebooking calls run in the background while the main thread — you — stays free. HTG Travels has run that loop from Sialkot for years, and nothing blocks.
The coffee-shop version
You order, take a buzzer, and sit down; the cashier serves the next customer; your name is called only when the counter is free; and the VIP line of pending promises always goes first.
The loop's philosophy — waiting without freezing — is embodied by Palestine's students, who keep their place in the queue through connectivity that arrives in fragments.
When an airline cancels a flight at 2 AM, our desk at HTG Travels runs a very human loop: check the queue, rebook, retry, confirm — until a family from Sialkot or Lahore is holding a real ticket rather than a promise. The waiting is our job, so it stops being yours. The working line is +92 317 5054959.




