One turn of the loop
Fromsrc/event/loop.zig (runWithMicrotasks), in order:
- Block for completions if referenced work is armed (a timer, socket,
fetch, query…). If only queued submissions remain, drain them without
blocking — that’s what lets an
unref()’d timer stop holding the process. - Drain subsystems in a fixed order: fetch → WebSocket client → FFI →
async
fs→crypto.bcrypt→ workers → child process → OS signals (process.on). Each drain settles Promises or invokes signal listeners on the JS thread. - Pump microtasks — every
queueMicrotaskcallback queued this turn. - GC check every 8192 turns, and only when the heap exceeds 8 MB.
- Exit or sleep: if nothing relevant is pending, the loop stops and the process exits. If it’s idle, it sleeps 100 µs doubling up to 8 ms.
Ordering guarantees
Timers and microtasks are ordered, and you can rely on it:- All synchronous code runs first — always.
- Microtasks run before the next timer or I/O callback.
setTimeout(fn, 0)runs on the next loop turn, which makes it the equivalent of Node’ssetImmediate.- Negative delays are clamped to
0; they defer, they never drop.
What keeps the process alive
The process exits when none of these are true:
An
unref()’d timer does not hold the process:
unref() it, and let the script end.
Async handlers park connections
Insidehttp.serve, an async handler is suspended — not blocking the
thread — until its Promise settles:
504 after 30 s. A thrown
error or rejected Promise becomes a 500.
When the process exits
Details and more examples: Errors and exit codes.
Gotchas
- There is no
process.nextTick— usequeueMicrotask. - Sync
fscalls block this thread. In a server, preferfs.*Asyncso a slow disk never stalls connections — but note the Known issue above: a successful*Asynccall still pins the process afterwards. - An infinite JS loop hangs the process — there is no watchdog that can interrupt running JavaScript. See Limitations.
consolewrites to stderr, soff app.js > out.txtcaptures nothing.- The loop wakes at most every 100 µs while idle; don’t expect sub-100 µs timer precision.
See also
- Concurrency — loop vs Workers vs the I/O pool
- Timers —
unref,refresh, limits - Errors and exit codes
- Limitations