Write sleep() with promises
Sooner or later you need to wait — before a retry, between polling attempts, or to let an animation finish. setTimeout alone does not compose with await, so teams wrap it once and reuse it everywhere.
Implement sleep(ms). It returns a Promise that resolves with undefined after at least ms milliseconds have passed, and it must never reject. sleep(0) still resolves asynchronously — code that runs after the call but before the promise settles must run first. Nothing about the page should be blocked while waiting, so a busy loop is not a solution.
What it has to do
- Return a real
Promiseinstance, even formsof 0. - Resolve after at least
msmilliseconds, never before. - Resolve asynchronously: any code that follows the call synchronously runs first.
- Several sleeps started at once must resolve in order of their durations.
Your workspace
Ready to check it?
4 tests run against your code, right here in your browser. Sign in to claim the XP when you pass.
AI Crack & Solution Assist
Stuck? Get instant AI hints or break down the optimal solution.
Stuck? The javascript course covers everything this challenge needs.