Timers

timers/promises.setInterval()

Produces an async iterable of timer ticks. Break out of iteration when the job finishes; it can also be canceled by a signal. A finite example is safer than an unbounded loop and does not promise a fixed cadence under load. Tested on Node.js 22.16.0. Example output (one line per entry): ["tick,tick"].

Formal Syntax & Structure

nodejs syntaxnodejs
setInterval([delay[, value[, options]]])

Parameters & Attributes

NameTypeDescription
delaynumberRequested milliseconds.
valueanyValue yielded per tick.
optionsobjectMay include signal and ref.

Return Value

AsyncIterator

Iterator yielding timer values.

Working Example

Example Snippetnodejs
import { setInterval } from 'node:timers/promises';const seen = [];for await (const v of setInterval(1, 'tick')) {    seen.push(v);    if (seen.length === 2)        break;}console.log(seen.join(','));
Node.js Reference