LAB / 01

Event Loop

call stack / microtasks / timers

Run this snippet and watch every line move through the call stack, the microtask queue and the task queue. Understanding the order of asynchronous work is the point — not memorising a diagram.

Conceptual modelThe core browser/Node.js ordering — not libuv's internal phases (I/O, setImmediate, nextTick).
example.js
console.log("A");

setTimeout(() => console.log("B"), 0);

Promise.resolve().then(() => console.log("C"));

console.log("D");

Call stack

— empty —

idle

Microtask queue

— empty —

Task / timer queue

— empty —

Output

  1. no output yet

Program loadedThe script is parsed. Nothing has executed yet.

The call stack runs synchronous code one context at a time. When it empties, the event loop drains the entire microtask queue (promise callbacks) before it picks up a single timer task. That is why the output is A, D, C, B — not A, B, C, D. Node.js follows the same core ordering; this experiment deliberately shows only that core, not libuv's I/O events, setImmediate or process.nextTick.