unref() and ref()
Methods on Node.js timers, sockets and servers: unref() lets the process exit even though this handle is still active, and ref() undoes that.
In more detail
They suit background work such as heartbeats or a shutdown fail-safe timer, which should never on their own keep a finished program running. They return the object itself, so a call can be chained, as in setTimeout(fn, 10_000).unref().
Related terms
- libuv handle — A libuv object representing a long-lived resource such as a timer, socket or server; while active ones remain, the event loop keeps the process alive.
- setTimeout — A JavaScript function that runs a callback once after at least the given number of milliseconds, unless clearTimeout cancels it first.
- graceful shutdown — Stopping a service in an orderly way: finish current work, close connections, then exit.
Taught in
- Timers Phase: setTimeout, Min-Heap Scheduling & Drift — Node.js Internals & Backend Services
- Production Hardening: Graceful Shutdown & Docker — Node.js Internals & Backend Services