Why can a promise callback run before a zero-delay timer, even when the timer was scheduled first? They do not wait in the same queue. Promise reactions wait as microtasks; the host decides when to drain them relative to other work. section 8.4.2 section 1.3.2
Listing 1.1 A friendly puzzle
function print(a) {
process.stdout.write(a);
}
queueMicrotask(() => print('e'));
process.nextTick(() => print('H'));
setTimeout(() => print('l'), 0);
Promise.resolve().then(() => print('l'));
setImmediate(() => print('o\n'));The callbacks are scheduled in source order, but Node.js CommonJS prints “Hello”: its nextTick queue drains before the microtasks, followed by the timer and immediate. The listing makes queue order visible one character at a time. section 1.1
ECMAScript introduced a job queue with promises; it does not specify timers, I/O callbacks, or an event loop. Runtimes commonly call the queue holding promise-reaction jobs the microtask queue. A resolved promise’s `.then()` callback is still queued rather than executed inline. `queueMicrotask()` also places a callback there. section 8.4 section 1.1
Do not picture one universal event-loop turn followed by one universal microtask drain. A host can drain the queue repeatedly within a turn. Node.js also keeps `process.nextTick()` callbacks in a separate queue, which it drains before promise reactions; `setImmediate()` has another queue. Those ordering choices belong to the runtime, not to ECMAScript. section 1.3.2 section 8.4.2
let drained = 0;
async function queueMore(depth) {
await Promise.resolve();
drained++;
if (depth > 0) await queueMore(depth - 1);
}
setTimeout(() => {
console.log(`microtasks drained before timer: ${drained}`);
}, 0);
queueMore(1000);An async function recursively awaits resolved promises while a zero-delay timer waits. In Node.js and Chrome, the timer reports 1001 drained microtasks, exposing how much work can run before it fires. section 1.3.2
When “end” arrives before the results
A stream’s `data` handler awaits a transform before appending its result. Its `end` handler then counts the results—but `end` signals that the stream has finished emitting data, not that async transforms have finished. Track the transform promises and `await Promise.all(pending)` before counting, or consume the stream with `for await`. Do not rely on a particular interleaving of stream events and microtask checkpoints. section 8.5.4
Queue ordering is also a diagnostic clue. If response times worsen during a burst of promise resolutions and CPU samples cluster in their callbacks, synchronous microtask draining may be keeping the event loop from other work. Check the runtime’s behavior rather than assuming results will match across hosts. section 11.5 section 1.1


