Skip to content
VaelJS Blog

node:timers

Node-compatible callback and promise timer utilities (node:timers).

Available in vjs only (not available in vjs-lite). Enabled by default unless --no-node-compat is passed. Never installed on globalThis — import explicitly from "node:timers".

import ... from "node:timers"
Since v0.0.0-alpha.2 vjs (V8)
Cargo feature: node-compat (enabled by default in vjs; absent in vjs-lite; disable with --no-node-compat)
import { setTimeout, clearTimeout } from "node:timers";
const handle = setTimeout(() => {
console.log("fired");
}, 10);
clearTimeout(handle);
functionSince v0.0.0-alpha.2vjs only
export function setTimeout<TArgs extends any[]>(
  callback: (...args: TArgs) => void,
  delay?: number,
  ...args: TArgs
): Timeout;

Schedule one-time execution of callback after delay milliseconds.

Parameters
NameTypeDescription
callback(...args: TArgs) => voidFunction invoked when the timer expires.
delay optionalnumberDelay in milliseconds (default 1).
...argsTArgsAdditional arguments forwarded to callback.
Returns
Timeout — Timeout handle for cancellation or ref management.
functionSince v0.0.0-alpha.2vjs only
export function clearTimeout(id: Timeout | string | number | undefined): void;

Cancel a Timeout scheduled by setTimeout.

Parameters
NameTypeDescription
idTimeout | string | number | undefinedTimeout handle or numeric timer ID to cancel.
Returns
void
functionSince v0.0.0-alpha.2vjs only
export function setInterval<TArgs extends any[]>(
  callback: (...args: TArgs) => void,
  delay?: number,
  ...args: TArgs
): Timeout;

Schedule repeated execution of callback every delay milliseconds.

Parameters
NameTypeDescription
callback(...args: TArgs) => voidFunction invoked on each interval tick.
delay optionalnumberInterval duration in milliseconds (default 1).
...argsTArgsAdditional arguments forwarded to callback.
Returns
Timeout — Timeout handle for cancellation or ref management.
functionSince v0.0.0-alpha.2vjs only
export function clearInterval(id: Timeout | string | number | undefined): void;

Cancel a repeating Timeout scheduled by setInterval.

Parameters
NameTypeDescription
idTimeout | string | number | undefinedTimeout handle or numeric timer ID to cancel.
Returns
void
functionSince v0.0.0-alpha.2vjs only
export function setImmediate<TArgs extends any[]>(
  callback: (...args: TArgs) => void,
  ...args: TArgs
): Immediate;

Schedule immediate execution of callback after the current I/O turn.

Parameters
NameTypeDescription
callback(...args: TArgs) => voidFunction invoked on the immediate turn.
...argsTArgsAdditional arguments forwarded to callback.
Returns
Immediate — Immediate handle for cancellation or ref management.
functionSince v0.0.0-alpha.2vjs only
export function clearImmediate(id: Immediate | undefined): void;

Cancel an Immediate scheduled by setImmediate.

Parameters
NameTypeDescription
idImmediate | undefinedImmediate handle to cancel.
Returns
void
interfaceSince v0.0.0-alpha.2vjs only
export interface AbortSignalLike {
  readonly aborted: boolean;
  readonly reason?: unknown;
  addEventListener(type: string, listener: (...args: any[]) => void, options?: { once?: boolean }): void;
  removeEventListener?(type: string, listener: (...args: any[]) => void): void;
}

Minimal abort-signal contract accepted by timers.promises helpers.

Properties
PropertyTypeDescriptionSince
aborted readonlybooleanWhether the signal has already been aborted.v0.0.0-alpha.2
reason readonly optionalunknownOptional abort reason value.v0.0.0-alpha.2
Methods
methodSince v0.0.0-alpha.2
addEventListener(type: string, listener: (...args: any[]) => void, options?: { once?: boolean }): void;

Register an "abort" listener.

Parameters
NameTypeDescription
typestringEvent type ("abort").
listener(...args: any[]) => voidCallback invoked on abort.
options optional{ once?: boolean }Optional listener options.
Returns
void
methodSince v0.0.0-alpha.2
removeEventListener?(type: string, listener: (...args: any[]) => void): void;

Unregister an "abort" listener.

Parameters
NameTypeDescription
typestringEvent type ("abort").
listener(...args: any[]) => voidCallback to remove.
Returns
void
interfaceSince v0.0.0-alpha.2vjs only
export interface TimerOptions {
  signal?: AbortSignalLike;
  ref?: boolean;
}

Options accepted by promise-based timer functions.

Properties
PropertyTypeDescriptionSince
signal optionalAbortSignalLikeAbort signal used to cancel the pending timer.v0.0.0-alpha.2
ref optionalbooleanKeep the event loop active while the timer is pending (default true).v0.0.0-alpha.2
interfaceSince v0.0.0-alpha.2vjs only
export interface Timeout {
  ref(): this;
  unref(): this;
  hasRef(): boolean;
  refresh(): this;
  [Symbol.dispose](): void;
}

Timer handle returned by setTimeout and setInterval.

Methods
methodSince v0.0.0-alpha.2
ref(): this;

Keep the event loop active while this timer remains scheduled.

Returns
this — this for chaining.
methodSince v0.0.0-alpha.2
unref(): this;

Allow the event loop to exit if this timer is the only pending work.

Returns
this — this for chaining.
methodSince v0.0.0-alpha.2
hasRef(): boolean;

Return true if this timer currently keeps the event loop alive.

Returns
boolean — true if ref'd, otherwise false.
methodSince v0.0.0-alpha.2
refresh(): this;

Reset the timer’s start time to now and reschedule it with the original delay.

Returns
this — this for chaining.
methodSince v0.0.0-alpha.2
[Symbol.dispose](): void;

Cancel the timer when disposed via using.

Returns
void
interfaceSince v0.0.0-alpha.2vjs only
export interface Immediate {
  ref(): this;
  unref(): this;
  hasRef(): boolean;
  [Symbol.dispose](): void;
}

Handle returned by setImmediate.

Methods
methodSince v0.0.0-alpha.2
ref(): this;

Keep the event loop active while this immediate remains scheduled.

Returns
this — this for chaining.
methodSince v0.0.0-alpha.2
unref(): this;

Allow the event loop to exit if this immediate is the only pending work.

Returns
this — this for chaining.
methodSince v0.0.0-alpha.2
hasRef(): boolean;

Return true if this immediate currently keeps the event loop alive.

Returns
boolean — true if ref'd, otherwise false.
methodSince v0.0.0-alpha.2
[Symbol.dispose](): void;

Cancel the immediate when disposed via using.

Returns
void
interfaceSince v0.0.0-alpha.2vjs only
export interface TimersScheduler {
  wait(delay?: number, options?: { signal?: AbortSignalLike }): Promise<void>;
  yield(): Promise<void>;
}

Scheduler utilities exposed on timers.promises.scheduler.

Methods
methodSince v0.0.0-alpha.2
wait(delay?: number, options?: { signal?: AbortSignalLike }): Promise<void>;

Resolve after delay milliseconds.

Parameters
NameTypeDescription
delay optionalnumberWait duration in milliseconds (default 0).
options optional{ signal?: AbortSignalLike }Optional abort signal configuration.
Returns
Promise<void> — Promise resolving after delay ms.
methodSince v0.0.0-alpha.2
yield(): Promise<void>;

Yield control to the event loop and resolve on a subsequent turn.

Returns
Promise<void> — Promise resolving after yielding.
interfaceSince v0.0.0-alpha.2vjs only
export interface TimersPromises {
  setTimeout<T = void>(delay?: number, value?: T, options?: TimerOptions): Promise<T>;
  setInterval<T = void>(
      delay?: number,
      value?: T,
      options?: TimerOptions,
    ): AsyncIterableIterator<T>;
  setImmediate<T = void>(value?: T, options?: TimerOptions): Promise<T>;
  readonly scheduler: TimersScheduler;
}

Promise-based timer API exposed as timers.promises (node:timers/promises).

Properties
PropertyTypeDescriptionSince
scheduler readonlyTimersSchedulerEvent-loop scheduling helpers (wait, yield).v0.0.0-alpha.2
Methods
methodSince v0.0.0-alpha.2
setTimeout<T = void>(delay?: number, value?: T, options?: TimerOptions): Promise<T>;

Return a Promise that fulfills with value after delay milliseconds.

Parameters
NameTypeDescription
delay optionalnumberDelay in milliseconds (default 1).
value optionalTOptional fulfillment value.
options optionalTimerOptionsOptional abort signal and ref settings.
Returns
Promise<T> — Promise resolving to value.
methodSince v0.0.0-alpha.2
setInterval<T = void>(
  delay?: number,
  value?: T,
  options?: TimerOptions,
): AsyncIterableIterator<T>;

Return an async iterator yielding value every delay milliseconds.

Parameters
NameTypeDescription
delay optionalnumberInterval duration in milliseconds (default 1).
value optionalTOptional yielded value.
options optionalTimerOptionsOptional abort signal and ref settings.
Returns
AsyncIterableIterator<T> — Async iterator yielding value on each tick.
methodSince v0.0.0-alpha.2
setImmediate<T = void>(value?: T, options?: TimerOptions): Promise<T>;

Return a Promise that fulfills with value after the current I/O turn.

Parameters
NameTypeDescription
value optionalTOptional fulfillment value.
options optionalTimerOptionsOptional abort signal and ref settings.
Returns
Promise<T> — Promise resolving to value.
constSince v0.0.0-alpha.2vjs only
export const promises: TimersPromises;

Promise-based timer API (node:timers/promises).

defaultSince v0.0.0-alpha.2vjs only
const _default: {
  /** Schedule a one-time timer. @since 0.0.0-alpha.2 */
  setTimeout: typeof setTimeout;
  /** Cancel a one-time timer. @since 0.0.0-alpha.2 */
  clearTimeout: typeof clearTimeout;
  /** Schedule a repeating interval timer. @since 0.0.0-alpha.2 */
  setInterval: typeof setInterval;
  /** Cancel a repeating interval timer. @since 0.0.0-alpha.2 */
  clearInterval: typeof clearInterval;
  /** Schedule an immediate callback. @since 0.0.0-alpha.2 */
  setImmediate: typeof setImmediate;
  /** Cancel an immediate callback. @since 0.0.0-alpha.2 */
  clearImmediate: typeof clearImmediate;
  /** Promise-based timer API (`node:timers/promises`). @since 0.0.0-alpha.2 */
  readonly promises: TimersPromises;
};
export default _default;

Default export object providing all node:timers helpers and promises.