liveBook · Manning

Event loop

As explained in JavaScript in Depth

The event loop is the scheduling mechanism that repeatedly selects queued work and dispatches it for execution in a JavaScript runtime.

Why can five short callbacks print `Hello` in one run and a different order in another? Their callbacks do not all execute when they are registered. The runtime schedules them, and its event loop determines when queued work runs. The answer can depend on the runtime, its version, and whether a Node.js file is loaded as CommonJS or an ECMAScript module. section 1.1

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 puzzle registers work through `queueMicrotask()`, `process.nextTick()`, `setTimeout()`, a promise reaction, and `setImmediate()`. Predicting the output requires reasoning about how those scheduling mechanisms interact, not just reading the calls from top to bottom. section 1.1

Think of the event loop as repeatedly taking events from a queue and running their associated actions. A browser button’s `onclick` code, for example, is scheduled in reaction to a click rather than executed when the page is parsed. Events are processed one at a time; on a given execution thread, JavaScript runs to completion before the loop moves to the next task. Scheduling future execution creates the appearance of asynchrony, but the loop’s implementation belongs to the host runtime, not to one universal JavaScript scheduler. section 1.3.1

Table 1.1 Running the same code in five different runtimes yields six different results
Runtime
Variation
Results
Node.js
CommonJS
Hello
Node.js
ECMAScript module
elHo
l
Deno
ECMAScript module
elHlo
Bun
ECMAScript module
Helo
L
Chrome
CommonJS
Error! process.nextTick does not exit.
Cloudflare Workers
ECMAScript module
eHllo

Compare the outputs before relying on an observed ordering. The same scheduling puzzle produces different results across Node.js module formats and other runtimes; one environment fails because a required API is unavailable. section 1.1

Queue behavior is also a performance concern. If promise-resolution callbacks keep the microtask queue draining, other work can wait and response times can rise. A burst of resolutions together with CPU-profile time in those callbacks is a clue to microtask starvation, even without an event-loop-delay monitor. section 11.5

Do not equate process-wide CPU usage with event-loop capacity. Node.js can perform work on background and thread-pool threads while its main JavaScript thread is idle. Conversely, synchronous computation can saturate the event loop while a four-core machine reports only 25% CPU usage. Reason about the work blocking the loop, not just the aggregate CPU number. section 11.4.1

How JavaScript in Depth covers it

How deep it goes
Explains queued events and run-to-completion, then applies queue behavior to starvation and diagnosis without specifying one universal runtime implementation.
Examples
Contrasts a callback-output puzzle across runtimes and module formats with cases where low aggregate CPU conceals a saturated loop.
What it assumes
Builds a queue-and-event intuition from a browser click while using promises and scheduling APIs for the deeper ordering puzzle.
What you can do after
Diagnose/debug with it by tracing callback ordering, recognizing microtask starvation, and separating event-loop saturation from process-wide CPU usage.
How it connects
Connects promises, microtasks, scheduling APIs, CPU utilization, and response latency.

Also explained in

Compare how 14 books explain event loop →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of JavaScript in Depth. The section links point to the parts of the book it draws on.