Timers

timeout.ref(), unref() and hasRef()

Changes or inspects whether a timer keeps the Node process alive. An unreferenced timer may never run if no other work keeps the process active. Do not use it to guarantee background task completion. Tested on Node.js 22.16.0. Example output (one line per entry): ["true", "false", "true"].

Formal Syntax & Structure

nodejs syntaxnodejs
timeout.unref(); timeout.ref(); timeout.hasRef()

Return Value

Timeout | boolean

ref/unref return the handle; hasRef returns a boolean.

Working Example

Example Snippetnodejs
const t = setTimeout(() => { }, 1000);try {    console.log(t.hasRef());    t.unref();    console.log(t.hasRef());    t.ref();    console.log(t.hasRef());}finally {    clearTimeout(t);}
Node.js Reference