codecks.

Async & promises · Output question

JavaScript catch and a recovered promise chain

A rejected promise is handled by catch. Predict how the returned recovery value moves through the next reaction.

Promise.reject("offline")
  .catch(() => "cached")
  .then(value => console.log(value));
console.log("request");

What gets logged?

Choose an output before opening the walkthrough, or check your answer in practice mode.

  1. offline
    request
  2. request
    offline
  3. request
    cached
  4. cached
    request
Reveal the answer and execution trace
request
cached

catch can recover by returning a value. The downstream then receives that value, after the synchronous script finishes.

  1. Line 1. The promise is rejected with "offline".
  2. Line 2. The rejection handler is queued as a microtask.
  3. Line 4. The script logs "request" first.
  4. Line 3. catch returns "cached", allowing then to log that value.

Reference: Promise recovery on MDN.