Skip to curriculum
Jump to the editor

14.1. Reject a promise after a time limit

Easy
Topics
10–15 min

A page waits for a promise: a request, a script loading, a worker answering. If that promise never settles, the spinner turns forever. A time limit turns "never" into a clear failure the page can handle: show an error, offer a retry, fall back to cached data.

Write withTimeout(promise, ms) in timeout.js. It returns a new promise that settles the way promise does if promise settles within ms milliseconds. If ms pass first, it rejects with an error named TimeoutError.

Signature

JAVASCRIPT
function withTimeout(promise, ms) // returns a Promise
  • promise: the work to wait for. A plain value (not a promise) counts as already finished.
  • ms: the time limit in milliseconds.
  • Returns a promise that fulfills with the promise's value, rejects with the promise's error, or rejects with a TimeoutError.

Requirements

  1. withTimeout returns a promise.
  2. If promise fulfills within the limit, the result fulfills with the same value, as soon as promise does.
  3. If promise rejects within the limit, the result rejects with the same error object.
  4. If ms pass first, the result rejects at exactly ms, not earlier, with an Error whose name is "TimeoutError" and whose message includes ms.
  5. Once the result has settled, a later outcome of promise changes nothing, and a late rejection is not reported as unhandled.
  6. The timer is cleared as soon as promise settles, whether it fulfilled or rejected.
  7. A plain value, or a promise that has already resolved, wins even against a 0 ms limit.
  8. Each call has its own timer.

Examples

JAVASCRIPT
const slow = new Promise((resolve) => setTimeout(() => resolve("data"), 500));

withTimeout(slow, 1000).then(console.log);
// at 500 ms: "data"

withTimeout(slow, 200).catch((error) => console.log(error.name, error.message));
// at 200 ms: "TimeoutError" "Timed out after 200 ms"
JAVASCRIPT
withTimeout(Promise.reject(new TypeError("bad")), 100).catch((error) => console.log(error.name));
// at once: "TypeError" (the promise's own error)
JAVASCRIPT
withTimeout(42, 0).then(console.log);
// 42: a plain value is already done, so the 0 ms timer never wins

Environment

  • Plain JavaScript in a browser page; the tests call the global withTimeout from timeout.js.
  • The tests run on a virtual clock that replaces setTimeout and clearTimeout, so use those.
  • Stopping the slow work itself is not part of this problem (a promise cannot be cancelled from outside): that is problem 14.2, with fetch and AbortController.
  • The wording of the message is up to you, as long as it includes ms.

Hints

  1. Which kind of race needs two contestants: the promise, and what else?
  2. A promise can only settle once. Does that mean you can call resolve and reject without checking who came first?

The explanation comes with the course

A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.

  • Every line explained
  • The mistakes each test catches
  • Links to the lessons behind it
Back to the question
Loading the editor…