Two Queues, Not One
The event loop does not have a single waiting line for callbacks. It has at least two, and one of them always goes first.
Web APIs run timers and network work outside the call stack. Finished callbacks wait in one of two queues: promises go to the microtask queue, timers and events go to the callback queue. The event loop drains microtasks completely before taking one callback.
EXERCISE
1What Runs Where
JavaScript executes one thing at a time, so everything that takes time has to happen somewhere else and come back later.
has one call stack. One thing runs at a time, start to finish, and nothing interrupts it. That should mean a one second timer freezes your page for a second, and it plainly does not.
The reason is that the timer never ran in JavaScript at all.
The pieces, named:
// CALL STACK Where your code actually runs. One frame at a time.
// WEB APIs Timers, network requests, file reads, DOM events.
// Provided by the browser or by Node, not by the language.
// QUEUES Where finished callbacks wait for the stack to be free.
// EVENT LOOP Moves one waiting callback onto the stack when it is empty.
Traced through a small example:
console.log("one");
setTimeout(function () {
console.log("three");
}, 0);
console.log("two");
// one
// two
// three
Step by step: console.log("one") runs on the stack and finishes. setTimeout runs on the stack, hands the callback and the delay to a Web , and returns immediately. The stack is now free for console.log("two"). Only when all of that top-level code has finished does the event loop take the waiting callback and run it.
three is last even with a zero delay, because the stack had to empty first. That is the whole mechanism.
Which is why blocking work really does freeze everything:
setTimeout(function () {
console.log("timer");
}, 0);
const start = Date.now();
while (Date.now() - start < 2000) {
// two seconds of synchronous work
}
console.log("loop finished");
// loop finished
// timer <- two seconds late
The timer was ready after zero milliseconds. The event loop could not run it, because the stack was busy and the event loop never interrupts a running frame.
Everything in this unit follows from that one rule: a callback runs only when the stack is empty.
Pro Tip: "Asynchronous" does not mean "at the same time". It means "later, when the stack is free". JavaScript is still doing one thing at a time; the waiting happens somewhere that is not JavaScript. Once you hold that distinction, async behaviour stops being about concurrency and becomes about ordering, which is something you can reason about precisely.
EXERCISE
2The Second Queue
Promise callbacks do not wait in the same line as timers, and the line they wait in has priority.
So far this describes one queue. There are two, and the difference between them is the most testable thing in this unit.
The two queues:
// CALLBACK QUEUE (also called the macrotask or task queue)
// setTimeout, setInterval, DOM events, network callbacks
// MICROTASK QUEUE
// promise callbacks (.then, .catch, .finally)
// queueMicrotask, and await resumption
// MutationObserver
And the rule that connects them:
// After the stack empties, the event loop:
// 1. drains the microtask queue COMPLETELY
// 2. takes ONE callback from the callback queue
// 3. drains the microtask queue completely again
// 4. repeats
Note step 1 says completely and step 2 says one. That asymmetry is the whole of the next lesson.
Seeing both queues in one run:
console.log("script start");
setTimeout(function () {
console.log("timeout");
}, 0);
Promise.resolve().then(function () {
console.log("promise");
});
console.log("script end");
// script start
// script end
// promise <- microtask, drained first
// timeout <- callback queue, after
Both callbacks were ready at the same moment. The promise ran first, and not because it was registered first, because it was registered second. It ran first because microtasks are drained before the event loop touches the callback queue at all.
The important word in that rule is completely. The loop does not take one microtask and move on; it keeps going until the queue is empty, and only then looks at the callback queue. That is why the next example behaves the way it does.
Microtasks queued from inside microtasks still go first:
console.log("start");
setTimeout(() => console.log("timeout"), 0);
Promise.resolve()
.then(() => {
console.log("promise 1");
return Promise.resolve();
})
.then(() => console.log("promise 2"));
console.log("end");
// start
// end
// promise 1
// promise 2 <- still before the timer
// timeout
promise 2 was not queued until promise 1 had already run, which was after the timer was waiting. It still went first, because the loop keeps draining microtasks until that queue is empty before it moves on.
Pro Tip: When you need to predict output, sort the work into three buckets in this order: synchronous code, then every microtask, then one macrotask at a time. Write the buckets down before reasoning about the code. Nearly every wrong answer to an event loop question comes from mixing the last two.
EXERCISE
3Where the Rule Comes From
The two-queue design is not an implementation quirk; it exists to give promises a guarantee they could not otherwise offer.
It is reasonable to ask why there are two queues rather than one. The answer explains several behaviours at once.
A promise has to settle before the browser does anything else:
const button = document.querySelector("button");
fetchUser()
.then((user) => {
button.textContent = user.name;
});
// If .then were a macrotask, the browser could render a frame between
// the fetch finishing and the text being set, and the user would see a
// flash of the old name. Because it is a microtask, all of the promise
// work completes before the browser gets a chance to paint.
Microtasks run at the end of the current task, before rendering, before the next timer, before anything else. That gives promise chains a useful property: a whole chain of .then calls settles as one uninterrupted burst, so no intermediate state is ever observable from outside.
It also makes promise ordering deterministic:
Promise.resolve().then(() => console.log("a"));
Promise.resolve().then(() => console.log("b"));
setTimeout(() => console.log("c"), 0);
setTimeout(() => console.log("d"), 0);
// a
// b
// c
// d
Promises run in the order they were queued, and all of them run before any timer. Without the separation, a slow timer registered earlier could interleave and the order would depend on timing rather than on the code.
queueMicrotask lets you use the queue directly:
console.log("start");
queueMicrotask(() => console.log("microtask"));
setTimeout(() => console.log("macrotask"), 0);
console.log("end");
// start
// end
// microtask
// macrotask
You will rarely need it. It exists for library authors who want work to happen after the current code but before the browser does anything else, without creating a promise just to get the timing.
And Node adds one more, which is worth recognising:
// process.nextTick(fn) queues ahead of even the microtask queue in Node.
// It is Node-specific, not part of the language, and the Node docs
// themselves recommend preferring queueMicrotask in new code.
The practical takeaway is a guarantee you can rely on: once a promise chain starts settling, nothing else gets in between. No timer, no click handler, no render. That is why a .then chain never shows a half-updated UI, and it is a property the single-queue design could not have provided.
