Wait for an amount of time

Return a promise that resolves after the given milliseconds.

JavaScriptNode.js & browsers Version 1.0.0 · MIT · .md

wait-for-an-amount-of-time.js
function wait(milliseconds) { return new Promise(resolve => setTimeout(resolve, milliseconds)); }

Usage

usage.js
const started = performance.now();
await wait(50);
// At least ~50 ms later (timers can fire a millisecond early):
performance.now() - started >= 45; // → true

Lines marked → were executed in Node.js 22 and in headless Chromium, and each result was compared with the value shown. Lines marked e.g. are illustrative.

Know the limits

  • setTimeout is a minimum delay, not a schedule: a busy event loop or a background tab can delay it much longer.
  • Node.js ships the same: import { setTimeout as wait } from "node:timers/promises", which also accepts an AbortSignal.
  • Typical use: await wait(retryAfterMs) before retrying a 429 or 503 response.

The function above is the code the usage lines run. It does not validate its arguments; add checks where it receives untrusted input.

Source & maintenance

Based on Wait for an amount of time from the original 1loc collection by Nguyen Huu Phuoc, with furkankose. Copyright (c) 2020 phuoc-ng. MIT licensed.

Documented as a reference with executed usage lines. Original MDX retained as inert source evidence.

Maintained by Oveyon. Source commit: 14b481cf60df. Read the license.

Usage lines executed in Node.js 22 and headless Chromium; no external services · reviewed October 28, 2026.

Keep exploring

Type two or more characters. Try “duplicate”, “webhook” or “retry”.