AniUI Academy
medium+250 XPPractice

Give a promise a deadline

Networks do not always fail loudly — sometimes a request simply never answers. Without a deadline your loading state is permanent, which is a worse experience than an honest error.

Implement withTimeout(value, ms, message). It returns a promise that fulfils with the resolved value of value if it settles within ms milliseconds, and rejects with that input's reason if the input rejects first. If neither happens within ms, reject with a new Error(message). When message is not supplied, use exactly "Timed out". A non-promise value counts as already fulfilled. Falsy values such as 0 must resolve normally rather than being treated as missing.

What it has to do

  • Fulfil with the input's value when it settles in time, including falsy values.
  • Reject with an Error carrying message once ms has elapsed.
  • Default the timeout message to Timed out.
  • Pass an input rejection straight through as the rejection reason.
  • Clear the pending timer once the input settles.

Your workspace

Try it yourself
Loading playground...

Ready to check it?

5 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.