CodePark/ questions / Promises & async/await
START PRACTICINGPRACTICE
Frontend question · JavaScript › Promises & async/await

What does this log?

Promise.resolve(1)
  .then((x) => x + 1)
  .then((x) => {
    throw new Error(String(x));
  })
  .then(() => console.log("skipped"))
  .catch((e) => {
    console.log("caught", e.message);
    return 10;
  })
  .then((x) => console.log("after", x));
caught 2, then after 10

The answer is "caught 2, then after 10".
• The value becomes
2 after the first then.
• The throw rejects the chain. The rejection skips the
then that logs skipped.
•
catch handles the error and logs caught 2. It returns 10.
• Returning a value from
catch fulfills the next promise, so the chain continues and logs after 10.

The tempting option "caught 2 only; the chain stops after catch" is wrong because
catch recovers the chain when it returns a value.