Handling rejected promises

For a rejected promise, we have two ways of handling it: we can either use the second callback in the .then() method, or we can use the .catch() method. Here are the two versions available to us:

// alternative 1getMoreData().then(  data => {     console.log('data',data);   },   err => {     console.log('error',err);   })// alternative 2getMoreData().then(data => {  console.log('data', data);}).catch((err) => {   console.log('error', err); }); 

In the first case, we have a second callback added to the then() method, and in the second version, we chain a catch() method to the existing then() method. They are equivalent so you can use either one, but only one.

Get Architecting Angular Applications with Redux, RxJS, and NgRx now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.