Fall back when a request is too slow
A widget that waits three seconds for fresh numbers feels broken. Showing slightly stale data after 300ms feels instant, and the fresh data can arrive later.
Implement withFallback(value, fallback, ms). It returns a promise that resolves with the resolved value of value if that happens within ms milliseconds. If ms elapses first, resolve with fallback. If value rejects before ms elapses, also resolve with fallback. A non-promise value is treated as already fulfilled. The returned promise must never reject.
What it has to do
- Resolve with the input's value when it fulfils within
ms. - Resolve with
fallbackwhenmselapses first. - Resolve with
fallbackwhen the input rejects. - Accept a plain value as well as a promise.
- Never reject.
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.