node:timers and node:timers/promises match Node’s timers, including
scheduler and AbortSignal support; ordering is covered under Timers.
Example
js
// timers.mjs
import { setInterval, setTimeout as sleep } from "node:timers/promises";
console.log(await sleep(10, "woke"));
let ticks = 0;
for await (const _ of setInterval(5)) {
if (++ticks === 3) break;
}
console.log("ticks", ticks);
const controller = new AbortController();
const pending = sleep(60_000, null, { signal: controller.signal });
controller.abort();
await pending.catch((error) => console.log(error.name));
utf-8 · js · Lines: 15
elide run timers.mjs prints:
text
woke
ticks 3
AbortError
utf-8 · text · Lines: 3
Differences from Node
node:timersfunctions are not the same objects as the timer globals.active()andunenroll()are not exposed.
Reference
Status: Full · 54 of 57 suite tests pass (94.7%) · since 1.1.0
Exports
| Export | Kind | Status | Notes |
|---|---|---|---|
setTimeout() | function | Full | Schedules a callback to be invoked once after delay milliseconds |
clearTimeout() | function | Full | Cancels a timeout previously established by setTimeout() |
setInterval() | function | Full | Schedules a callback to be invoked repeatedly every delay milliseconds |
clearInterval() | function | Full | Cancels an interval previously established by setInterval() |
setImmediate() | function | Full | Schedules a callback to be invoked asynchronously “immediately” (after I/O events but before timers… |
clearImmediate() | function | Full | Cancels an immediate action previously established by setImmediate() |
Tests: node-api node:timers: 54 pass, 3 fail, 0 error, 10 skipped. Failing: test-timers-max-duration-warning.js, test-timers-timeout-to-interval.js, test-timers-unenroll-unref-interval.js.