Sleep / Delay Promise

easy
By FrontendAtlas Editorial · Updated Oct 10, 2026
Implement sleep(ms) that returns a Promise resolving after ms milliseconds. Use it to pause async flows like retries, exponential backoff, UI delays, or test helpers without blocking the event loop or freezing the UI thread.

Arguments

  • ms: number — The delay in milliseconds.

Returns

Promise<void> — A promise that resolves after the delay.

Solution idea

This utility wraps setTimeout in a Promise so you can pause async control flow without blocking. It’s a common primitive for polling, retry logic, exponential backoff, and UI timing. The function should integrate cleanly with async/await, return a Promise every time, and behave predictably for zero or negative delays.

Examples
// Usage with async/await
await sleep(50);
console.log('done');

// Usage with then
sleep(20).then(() => console.log('ready'));