Experiments · JavaScript: From Closures to Autonomous Systems · Chapter 9
Queue order and a busy loop
The JavaScript event loop, microtasks and timers
Everything is computed in your browser; nothing is sent anywhere.
About this experiment
What is the event loop in JavaScript?
The event loop is how a single-threaded JavaScript runtime decides what runs next. The code that is running is never interrupted. When it finishes, the runtime drains the microtask queue, which holds promise continuations and queueMicrotask callbacks, to the very end. Only then does the loop take the next task: a timer, an incoming message, a finished read, and in a browser also the rendering of a frame. A callback that runs too long, or a microtask queue that keeps refilling itself, holds back every timer and every other callback until it is done.
JavaScript has one thread, and the runtime decides when each piece of code gets it. Work can be queued in several ways, and the queues are emptied in a fixed order: first the synchronous code to the end, then all the microtasks, promise continuations included, and only then the next task from the timers, messages, input and output. The order inside that last group is partly left to the implementation.
The first tab puts six lines in the order you expect and runs the code live: a setTimeout with zero delay, requestAnimationFrame, a MessageChannel message, promise.then, queueMicrotask and a synchronous log. Twenty runs show which part of the order holds every time and which may move. The second tab runs the book’s block.js in your browser: a 10 ms timer for two seconds, measuring how late each tick is while the loop is idle, busy for 50 ms every 100 ms, or flooded with a promise chain for one second. The last mode freezes the page for that second.
In the book, on Node 24, the order was sync code, process.nextTick, promise.then, queueMicrotask, setTimeout 0, setImmediate, then the readFile callback, inside which setImmediate came before setTimeout 0. The timer beat setImmediate in the main module 40 times out of 40, and the Node documentation still calls that order undefined. The timer was late by a median of 1.0 ms in every mode; the 99th percentile was 1.1 ms idle, 49.3 ms with the busy work and 990.1 ms with the promise chain, which made about 30 million turns in that second.
The chapter’s point is the duty the runtime leaves to the programmer. Scheduling belongs to the event loop, and in exchange no other code touches your data while yours is running, so no locks are needed. What is left is one hard rule, do not hold the loop, and it can be broken without any long function, by an endless chain of instant promises. Nothing checks it: the server just starts answering everyone else more slowly.
From the book
JavaScript: From Closures to Autonomous Systems · Chapter 9
The Event Loop
Node’s documentation does not hide this. In the main module, it says, the order of a zero-delay timer and setImmediate is undefined and depends on how fast the process is running at that moment, while inside an I/O callback setImmediate always comes first. On my machine the timer in the main module beat setImmediate in all forty runs out of forty, and that is a dangerous statistic. A programmer who checked the order forty times on their own machine and built logic on it will one day learn that on a loaded server the order is different. The only order you can rely on is the one written in the documentation. Whatever happened on your machine guarantees nothing.