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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.2.78