FrontendAtlas
Interview Questions
Home>JavaScript interview questions> JavaScript coding challenges >Sleep / Delay Promise

Sleep / Delay Promise

easy
By FrontendAtlas Editorial · Updated Jan 30, 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.
Examples
// Usage with async/await
await sleep(50);
console.log('done');

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

Solution

Overview

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.

1

Approach: Promise + setTimeout

Return new Promise(resolve => setTimeout(resolve, ms)). Ensure you return the Promise so callers can await it or chain then. If ms is negative or NaN, setTimeout resolves immediately, which is acceptable for this helper. Keep the function tiny and deterministic; it should never block the main thread.

export default function sleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}
export default function sleep(ms: number): Promise<void> {
  return new Promise(resolve => setTimeout(resolve, ms));
}

Notes & Pitfalls

Pitfalls
  • Don't forget to return the Promise (otherwise callers cannot await it).
  • `setTimeout` returns a timer id, not a Promise.
  • Negative or NaN delays are treated as 0 by most environments.
Edge cases
  • `ms = 0` resolves on the next tick.
  • Very large delays may be clamped by the environment.
Techniques
  • Promise construction around callback-based APIs.
  • Basic async timing utilities.

Common mistakes on this challenge

  • sleep must return a Promise

    The function contract should always be Promise-based.

    • Return `new Promise((resolve) => { ... })` directly from `sleep`.
    • Resolve inside `setTimeout`, do not return from inside the callback only.
    • Re-run and verify the "returns a promise" test passes before timing tests.
  • Delay timing is too short or inconsistent

    The Promise resolves before the expected minimum delay.

    • Use `setTimeout(resolve, ms)` with the same `ms` input, no unit conversion.
    • Avoid immediate resolve paths that bypass the timer.
    • Measure elapsed time around `await sleep(ms)` to confirm it is at least `ms`.

Resources

  • MDN – setTimeout()
  • MDN – Promise

Similar questions

Poll Until (with Timeout + Abort)intermediateUpdate Array at Index (Immutable)easyFlatten One Leveleasy

Guides

Frontend interview preparation guideGuideFrontend coding interview questions and prep guideBlueprintJavaScript Problems That Actually Show UpBlueprintBuild Great UI in 60 MinutesBlueprint

Preparing for interviews? Use Frontend Coding Challenges first, then move into a concrete Study Plan before targeted Company Prep.

Open frontend interview questionsBrowse JavaScript interview questionsOpen Essential 60Open Machine Coding HubOpen Frontend Coding ChallengesOpen System DesignOpen Interview Prep GuideOpen System Design BlueprintOpen Framework Prep PathsOpen Study PlansOpen JavaScript mastery study planOpen Company PrepOpen JavaScript Framework Prep Guide
↗Incidents hub
← Prev←30 / 88