Timers
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.
setTimeout(fn, ms) / clearTimeout(id)
ExactSchedules 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.
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)setInterval(fn, ms) / clearInterval(id)
ExactSchedules 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.
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)setImmediate(fn) / clearImmediate(id)
ExactSchedules 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.
NodeJS.Immediate — Node's Immediate object: ref/unref/hasRef, usable with clearImmediate.
setImmediate(() => { console.log("immediate fired") })
const cancelledImmediateId = setImmediate(() => {})
clearImmediate(cancelledImmediateId)queueMicrotask(fn)
ExactQueues 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.
void — Nothing.
timers/promises
ExactThe 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.
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'))