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.
Reveal the answer and execution trace
request
cachedcatch can recover by returning a value. The downstream then receives that value, after the synchronous script finishes.
- Line 1. The promise is rejected with "offline".
- Line 2. The rejection handler is queued as a microtask.
- Line 4. The script logs "request" first.
- Line 3. catch returns "cached", allowing then to log that value.
Reference: Promise recovery on MDN.