Timers Promises API
History
The timers/promises API provides an alternative set of timer functions
that return Promise objects. The API is accessible via
require('node:timers/promises').
import { setTimeout, setImmediate, setInterval, } from 'node:timers/promises';
const { setTimeout, setImmediate, setInterval, } = require('node:timers/promises');
timersPromises.setTimeout(delay?, value?, options?): void
number1.anyObjectbooleanfalse to indicate that the scheduled Timeout
should not require the Node.js event loop to remain active.
Default: true.AbortSignalAbortSignal that can be used to
cancel the scheduled Timeout.import { setTimeout, } from 'node:timers/promises'; const res = await setTimeout(100, 'result'); console.log(res); // Prints 'result'
const { setTimeout, } = require('node:timers/promises'); setTimeout(100, 'result').then((res) => { console.log(res); // Prints 'result' });
timersPromises.setImmediate(value?, options?): void
anyObjectbooleanfalse to indicate that the scheduled Immediate
should not require the Node.js event loop to remain active.
Default: true.AbortSignalAbortSignal that can be used to
cancel the scheduled Immediate.import { setImmediate, } from 'node:timers/promises'; const res = await setImmediate('result'); console.log(res); // Prints 'result'
const { setImmediate, } = require('node:timers/promises'); setImmediate('result').then((res) => { console.log(res); // Prints 'result' });
timersPromises.setInterval(delay?, value?, options?): void
Returns an async iterator that generates values in an interval of delay ms.
If ref is true, you need to call next() of async iterator explicitly
or implicitly to keep the event loop alive.
number1.anyObjectbooleanfalse to indicate that the scheduled Timeout
between iterations should not require the Node.js event loop to
remain active.
Default: true.AbortSignalAbortSignal that can be used to
cancel the scheduled Timeout between operations.import { setInterval, } from 'node:timers/promises'; const interval = 100; for await (const startTime of setInterval(interval, Date.now())) { const now = Date.now(); console.log(now); if ((now - startTime) > 1000) break; } console.log(Date.now());
const { setInterval, } = require('node:timers/promises'); const interval = 100; (async function() { for await (const startTime of setInterval(interval, Date.now())) { const now = Date.now(); console.log(now); if ((now - startTime) > 1000) break; } console.log(Date.now()); })();
timersPromises.scheduler.wait(delay, options?): Promise
numberObjectbooleanfalse to indicate that the scheduled Timeout
should not require the Node.js event loop to remain active.
Default: true.AbortSignalAbortSignal that can be used to
cancel waiting.PromiseAn experimental API defined by the Scheduling APIs draft specification being developed as a standard Web Platform API.
Calling timersPromises.scheduler.wait(delay, options) is equivalent
to calling timersPromises.setTimeout(delay, undefined, options).
import { scheduler } from 'node:timers/promises'; await scheduler.wait(1000); // Wait one second before continuing
timersPromises.scheduler.yield(): Promise
PromiseAn experimental API defined by the Scheduling APIs draft specification being developed as a standard Web Platform API.
Calling timersPromises.scheduler.yield() is equivalent to calling
timersPromises.setImmediate() with no arguments.