Skip to content

Function: sleep()

sleep(ms, signal?): Promise<void>

Promisified setTimeout that wakes up early (and rejects with signal.reason) when signal aborts mid-wait. Short-circuits to a rejected promise when the signal is already aborted on entry, so the abort path is consistent regardless of whether the wait actually started.

Use as the building block for any “wait, but cancel cleanly” pattern - inter-poll backoff, pacing loops, retry timers, long-poll budgets - so cancellation always rejects with the caller’s signal.reason rather than silently resolving after the timer expires.

number

AbortSignal

Promise<void>

await sleep(250, req.signal);