Time Out a Promise with a Typed Error

Write the code, see it run, pass the checks.

The problem

  • easy
  • Code

Export a class TimeoutError that extends Error, with the name TimeoutError and a numeric ms property. Then write withTimeout(promise, ms): resolve or reject as the promise does if it settles within ms milliseconds, otherwise reject with a TimeoutError for that wait. Type it so the result has the promise's own value type, the first argument must be a promise and the wait a number.

Your code must pass

  • Resolves with the value when it's fast enough
  • Rejects with a TimeoutError when it's too slow
  • Passes on the promise's own error
  • Has no type errors
  • Resolves with the promise's type
  • Rejects a non-promise or a non-numeric wait
  • TimeoutError carries the wait