Time Out a Slow Promise
Write the code, see it run, pass the checks.
The problem
- medium
- Code
Write withTimeout(promise, ms): return a promise that settles the same way as promise if promise settles within ms milliseconds. If it hasn't by then, reject with an Error whose name is "TimeoutError" and whose message is "Timed out after 30 ms" (with ms in place of 30), and ignore whatever promise does later. Start the timer with setTimeout, and when promise settles in time, clear it with clearTimeout so nothing is left running. The checks use waits of 30 to 100 milliseconds.
Your code must pass
- Resolves with the value when it arrives in time
- Passes on a rejection that arrives in time
- Rejects with a TimeoutError when time runs out
- Clears its timer when the promise settles in time