JS: Promise .prototype .catch

By Xah Lee. Date: . Last updated: .

Promise .prototype .catch

(new in ECMAScript 2015)

promise_object.catch(onRejected)

equivalent to

promise_object.then( undefined, onRejected)

What's the difference between p.then(aa, bb) vs p.then(aa).catch(cc)

the p.then(aa, bb) returns a promise, and that promise will reject if aa or bb throws.

in p.then(aa).catch(cc), the catch will reject if aa throws, thus it'll call cc.

in p.then(aa,bb).catch(cc), the catch will reject if either aa or bb throws, thus it'll call cc.

in general, if your on_fulfilled function may throw, then it's a good idea to also attach a catch.