Skip to main content

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";

Functions

f
setImmediate
No documentation available
    f
    setInterval

    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.

      f
      setTimeout
      No documentation available

        Interfaces

        I
        Scheduler
        No documentation available

        Variables

        v
        scheduler
        No documentation available

          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
          optional

          A value with which the promise is fulfilled.

          #options: TimerOptions
          optional

          Return Type #

          Promise<T>

          function setInterval

          Usage in Deno

          import { setInterval } from "node:timers/promises";
          
          #setInterval<T = void>(
          delay?: number,
          value?: T,
          options?: TimerOptions,
          ): 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
          optional

          The number of milliseconds to wait between iterations. Default: 1.

          #value: T
          optional

          A value with which the iterator returns.

          #options: TimerOptions
          optional

          Return Type #

          AsyncIterator<T>

          function setTimeout

          Usage in Deno

          import { setTimeout } from "node:timers/promises";
          
          #setTimeout<T = void>(
          delay?: number,
          value?: T,
          options?: TimerOptions,
          ): 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
          optional

          The number of milliseconds to wait before fulfilling the promise. Default: 1.

          #value: T
          optional

          A value with which the promise is fulfilled.

          #options: TimerOptions
          optional

          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.


          variable scheduler

          Usage in Deno

          import { scheduler } from "node:timers/promises";
          

          Did you find what you needed?

          Privacy policy