- Architecting Angular Applications with Redux,RxJS,and NgRx
- Christoffer Noring
- 118字
- 2021-08-27 19:56:24
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 1
getMoreData().then(
data => {
console.log('data',data);
},
err => {
console.log('error',err);
}
)
// alternative 2
getMoreData().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.