JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 118
  • Score
    100M100P100Q82213F
  • License MIT

A modern, zero-dependency wait / timing utility toolkit for JavaScript and TypeScript.

Package Exports

  • wait-utils

Readme

wait-utils

A modern, zero-dependency wait and timing utility toolkit for JavaScript and TypeScript.

Supports AbortSignal, timeouts, retries, and polling โ€” all with first-class TypeScript types.

Version Maintenance License Codecov Open Bundle

Features

  • ๐Ÿ•’ Accurate delays with timeout, wait and waitUntil
  • ๐Ÿ” Flexible polling and retry logic with waitFor
  • ๐Ÿงช 100% test coverage โ€” reliable and verified
  • ๐Ÿงฉ Fully typed โ€” TypeScript support with minimal generics
  • ๐Ÿง˜ Zero dependencies โ€” lean and tree-shakable for any runtime

Installation

NPM:

npm install wait-utils

Yarn:

yarn add wait-utils

JSR:

jsr add @rojas/wait-utils

API

timeout(delay?, signal?): Promise<void>

Rejects with a TimeoutError after the specified delay, unless cancelled by an AbortSignal.

Parameters

Name Type Description
delay? number | null The number of milliseconds to wait before timing out
signal? AbortSignal Allows canceling the timeout

Returns

A Promise<void> that:

  • Resolves if the signal is aborted before the delay
  • Rejects with a TimeoutError if the full delay completes
  • Rethrows an unexpected error if one occurs

Example

import { timeout } from "wait-utils";

const controller = new AbortController();

try {
  await timeout(200, controller.signal);
  console.log("Aborted before timeout.");
} catch (error) {
  if (error.name === "TimeoutError") {
    console.error("Timed out!");
  } else {
    console.error("Unexpected error:", error);
  }
}

wait(delay?, signal?): Promise<void>

Waits for a given number of milliseconds, unless cancelled by an AbortSignal.

Parameters

Name Type Description
delay? number | null The number of milliseconds to wait
signal? AbortSignal Allows canceling the wait early

Returns

A Promise<void> that:

  • Resolves after the delay
  • Rejects with the AbortSignal.reason if cancelled before the delay

Example

import { wait } from "wait-utils";

await wait(1000); // waits 1 second

const controller = new AbortController();
wait(5000, controller.signal).catch((err) => {
  if (err.name === "AbortError") {
    console.log("Aborted!");
  }
});
controller.abort(); // cancels the wait

waitFor(delay | callback, options?): Promise<void>

Repeats a wait until a condition resolves or aborts

Parameters

Name Type Description
delay number | () => number | Promise<number> Fixed delay or a function that returns the delay in ms. Return โ‰ค 0 to resolve.
options? WaitForOptions Optional settings

Supported options are:

Option Type Description
signal? AbortSignal Cancels the wait early
timeout? number (ms) Max total time before a TimeoutError is thrown
onRetry? (ms: number) => boolean|void Called before each wait; return false to stop and throw RetryError

Returns

A Promise<void> that:

  • Resolves once the delay is โ‰ค 0
  • Rejects with the AbortSignal.reason if cancelled before the delay
  • Rejects with a RetryError if onRetry returns false
  • Rejects with a TimeoutError if the timeout is triggered before resolving

Examples

Fixed delay:

// Retry every 500ms, until the timeout
await waitFor(500, {
  timeout: 2000,
  onRetry(ms) {
    console.log(`Retrying in ${ms}ms...`);
  },
});

Dynamic delay:

import { waitFor } from "wait-utils";

// 80% wait for 100ms, 20% stop waiting
await waitFor(() => (Math.random() < 0.2 ? 0 : 100));

waitUntil(timestamp?, signal?): Promise<void>

Waits until the given performance.now() timestamp is reached, unless canceled by an AbortSignal.

Parameters

Name Type Description
timestamp? number | null Target timestamp (in milliseconds) relative to performance.now()
signal? AbortSignal Allows canceling the wait early

Returns

A Promise<void> that:

  • Resolves after the given timestamp is reached
  • Rejects with the AbortSignal.reason if cancelled before the delay

Example

import { waitUntil } from "wait-utils";

const target = performance.now() + 2000;
await waitUntil(target); // waits until ~2s have passed

Errors

  • AbortError
  • TimeoutError
  • RetryError

License

MIT ยฉ Michael Rojas

If you find this useful, consider sponsoring on GitHub.