Notes › JavaScript
The JavaScript Event Loop
JavaScript runs on one thread — it does one thing at a time. Yet it handles timers, network requests and clicks without freezing. The event loop makes that possible.
The pieces
| Piece | What it does |
|---|---|
| Call stack | runs functions one at a time (last in, first out) |
| Web APIs / Node APIs | the browser or Node does the waiting: timers, fetch, DOM events, file I/O |
| Macrotask (task) queue | callbacks from setTimeout, setInterval, I/O, UI events |
| Microtask queue | promise callbacks (.then, await continuation), queueMicrotask, MutationObserver |
| Event loop | when the stack is empty, runs all microtasks, then one macrotask, then repeats |
The rule that solves every output question
- Run the current synchronous code until the call stack is empty.
- Run every microtask (including new ones added meanwhile).
- Render (in the browser), then take one macrotask and go back to step 1.
console.log("1");
setTimeout(() => console.log("2"), 0);
Promise.resolve().then(() => console.log("3"));
console.log("4");
// Output: 1, 4, 3, 2
1and4are synchronous.3is a microtask → runs as soon as the stack is empty.2is a macrotask → runs after all microtasks, even with a 0 ms delay.
async/await is promises
Code after await is put in the microtask queue.
async function f() {
console.log("A");
await null; // pause: the rest becomes a microtask
console.log("B");
}
console.log("start");
f();
console.log("end");
// start, A, end, B
Details of promises: JavaScript Promises & Async/Await.
A harder puzzle
setTimeout(() => console.log("t1"), 0);
Promise.resolve()
.then(() => {
console.log("p1");
setTimeout(() => console.log("t2"), 0);
})
.then(() => console.log("p2"));
queueMicrotask(() => console.log("m1"));
console.log("sync");
Output: sync, p1, m1, p2, t1, t2
- sync first; microtasks queued in order: p1, m1; p1 queues p2 (a new microtask, still run before any timer); then the macrotasks t1, t2.
setTimeout delay is a minimum
setTimeout(fn, 100) means "not before 100 ms". If the stack is busy (a long loop), the callback waits longer.
Blocking the event loop
const start = Date.now();
while (Date.now() - start < 3000) {} // 3 s of busy work freezes clicks, rendering, timers
Fixes: split heavy work into chunks (setTimeout), use a Web Worker (browser) or worker_threads (Node), or move CPU work to the server.
Node.js event loop phases (overview)
timers → pending callbacks → poll (I/O) → check (setImmediate) → close callbacks. Between phases Node runs microtasks; process.nextTick callbacks run even before promise microtasks.
setImmediate(() => console.log("immediate"));
process.nextTick(() => console.log("nextTick"));
Promise.resolve().then(() => console.log("promise"));
// nextTick, promise, immediate
Interview one-liners
- JavaScript is single-threaded; concurrency comes from the event loop + host APIs.
- Microtasks (promises) always run before the next macrotask (timers).
setTimeout(fn, 0)is not immediate — it waits for the stack and all microtasks.awaitgives the rest of the function to the microtask queue.- Long synchronous code blocks everything — rendering, input and timers.