How does promise chaining work, and what happens after a catch?
Every then returns a new promise, resolved with whatever the handler returns, so calls chain. A thrown error skips later then handlers until a catch, and after a catch that returns a value, the chain continues normally.
Explain it simply
A promise chain is a relay race. Each .then takes the baton (the previous result), does its work, and passes a new baton to the next runner. If a runner trips (throws), the next runners that only handle success are skipped, until someone with a .catch picks up the fall.
Two key rules: whatever you return from a handler becomes the next value, even a promise, which is waited for. And a catch that returns normally recovers the chain, so later .then calls run again.
Forgetting return is the classic bug: the next step then gets undefined.
Example
Promise.resolve(1)
.then((v) => v + 1)
.then((v) => {
console.log(v); // Output: 2
throw new Error("boom");
})
.then(() => console.log("skipped"))
.catch((e) => {
console.log(e.message); // Output: boom
return "recovered";
})
.then((v) => console.log(v)); // Output: recoveredWhat to say in the interview
"Each then returns a new promise whose value is the handler's return value, which is why chaining works. Errors skip success handlers until a catch. If the catch returns a value, the chain recovers and continues. I always return inside handlers."
Common mistakes
- Forgetting to return inside then, so the next handler receives undefined.
- Nesting thens inside thens instead of flattening the chain.