timers/promises
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';
Usage in Deno
import * as mod from "node:timers/promises";
function setImmediate
Usage in Deno
import { setImmediate } from "node:timers/promises";
setImmediate<T = void>(
value?: T,
options?: TimerOptions,
): Promise<T>import {
setImmediate,
} from 'node:timers/promises';
const res = await setImmediate('result');
console.log(res); // Prints 'result'
Type Parameters
T = void Parameters
value: T A value with which the promise is fulfilled.
options: TimerOptions Return Type
Promise<T> function setInterval
Usage in Deno
import { setInterval } from "node:timers/promises";
setInterval<T = void>(): AsyncIterator<T>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.
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());
Type Parameters
T = void Parameters
delay: number The number of milliseconds to wait between iterations. Default: 1.
value: T A value with which the iterator returns.
options: TimerOptions Return Type
AsyncIterator<T> function setTimeout
Usage in Deno
import { setTimeout } from "node:timers/promises";
setTimeout<T = void>(): Promise<T>import {
setTimeout,
} from 'node:timers/promises';
const res = await setTimeout(100, 'result');
console.log(res); // Prints 'result'
Type Parameters
T = void Parameters
delay: number The number of milliseconds to wait before fulfilling the promise. Default: 1.
value: T A value with which the promise is fulfilled.
options: TimerOptions Return Type
Promise<T> interface Scheduler
Usage in Deno
import { type Scheduler } from "node:timers/promises";
Methods
wait(
delay: number,
options?: { signal?: AbortSignal; },
): Promise<void> An experimental API defined by the Scheduling APIs draft specification being developed as a standard Web Platform API.
Calling timersPromises.scheduler.wait(delay, options) is roughly equivalent to calling timersPromises.setTimeout(delay, undefined, options) except that the ref option is not supported.
import { scheduler } from 'node:timers/promises';
await scheduler.wait(1000); // Wait one second before continuing
yield(): Promise<void> An 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.
© 2018–2025 the Deno authors
Licensed under the MIT License.
https://docs.deno.com/api/node/timers/promises