API reference

Timers

5/5Coverage5/5Strict (zero caveats)

WHATWG/browser-standard timer functions as bare globals, matching real JS rather than a namespace. They are Node's own: Timeout and Immediate objects, timers of one duration sharing a list, one libuv timer armed for the earliest list, and immediates run in the loop's check phase after I/O; queueMicrotask has its own FIFO, drained after each callback.

Exact Works, with caveats Not supported

setTimeout(fn, ms) / clearTimeout(id)

Exact
setTimeout<TArgs extends any[]>(fn: (...args: TArgs) => void, ms?: number, ...args: TArgs): NodeJS.Timeout; clearTimeout(timeout: NodeJS.Timeout | string | number | undefined): void

Schedules a callback to fire once after ms milliseconds passing it any arguments after ms, and returns an id; clearTimeout cancels it before it fires. These are bare global functions, matching real JS (not a namespace). Multiple timeouts fire in delay order, not registration order.

Parameters
fn(...args: TArgs) => void
Callback; any function value, its result ignored.
msnumber
Delay in milliseconds.
argsTArgs
Arguments for fn, evaluated when the timer is scheduled.
Returns

NodeJS.Timeout — Node's Timeout object: ref/unref/hasRef/refresh/close, and +timeout as its id, which clearTimeout also takes.

setTimeout(() => { console.log("timeout fired") }, 10)
const cancelledId = setTimeout(() => {}, 8)
clearTimeout(cancelledId)
See it in a full example →

setInterval(fn, ms) / clearInterval(id)

Exact
setInterval<TArgs extends any[]>(fn: (...args: TArgs) => void, ms?: number, ...args: TArgs): NodeJS.Timeout; clearInterval(id: NodeJS.Timeout | string | number | undefined): void

Schedules a callback to repeat every ms milliseconds until cleared. Closures capture by reference, so the idiomatic self-cancelling pattern — a callback that reads the id its own declaration produces — works correctly. Same callback scope as setTimeout.

Parameters
fn(...args: TArgs) => void
Callback; any function value, its result ignored.
msnumber
Interval in milliseconds.
argsTArgs
Arguments for fn on every tick, evaluated once at the schedule.
Returns

NodeJS.Timeout — Node's Timeout object, usable with clearInterval.

let count: number = 0
const id = setInterval(() => {
    count = count + 1
    if (count >= 3) { clearInterval(id) }
}, 5)
See it in a full example →

setImmediate(fn) / clearImmediate(id)

Exact
setImmediate<TArgs extends any[]>(fn: (...args: TArgs) => void, ...args: TArgs): NodeJS.Immediate; clearImmediate(immediate: NodeJS.Immediate | undefined): void

Schedules a callback for the loop's check phase, after I/O polling, as Node does: scheduled from an I/O callback it runs before a 0 ms setTimeout.

Parameters
fn(...args: TArgs) => void
Callback; any function value, its result ignored.
argsTArgs
Arguments for fn, evaluated when the immediate is scheduled.
Returns

NodeJS.Immediate — Node's Immediate object: ref/unref/hasRef, usable with clearImmediate.

setImmediate(() => { console.log("immediate fired") })
const cancelledImmediateId = setImmediate(() => {})
clearImmediate(cancelledImmediateId)
See it in a full example →

queueMicrotask(fn)

Exact
queueMicrotask(fn: () => void): void

Queues a callback onto a real microtask FIFO, distinct from the timer queue, drained after the synchronous script and before any timers fire.

Parameters
fn() => void
Zero-argument, void-returning callback.
Returns

void — Nothing.

timers/promises

Exact
setTimeout<T = void>(delay?: number, value?: T, options?: TimerOptions): Promise<T>; setImmediate<T = void>(value?: T, options?: TimerOptions): Promise<T>; setInterval<T = void>(delay?: number, value?: T, options?: TimerOptions): AsyncGenerator<T>

The promise forms of the timers. setTimeout and setImmediate resolve with their value; setInterval is an async iterator that yields its value on every tick until the loop breaks. An AbortSignal in options rejects the pending timer with an AbortError, and ref: false lets the process exit while it waits. scheduler.wait and scheduler.yield are setTimeout and setImmediate without a value.

Parameters
delaynumber
Milliseconds to wait (1 when omitted).
valueT
What the promise resolves with, or the iterator yields.
optionsTimerOptions
{ signal?: AbortSignal, ref?: boolean }, validated as Node does.
Returns

Promise<T> | AsyncGenerator<T> — Settles when the timer fires or its signal aborts.

import { setTimeout as sleep } from 'timers/promises'
console.log(await sleep(10, 'kalimera'))
See it in a full example →