官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 余姚市| 上高县| 阆中市| 深水埗区| 石屏县| 嘉祥县| 绥滨县| 出国| 莎车县| 石家庄市| 贺州市| 磴口县| 临潭县| 剑河县| 玛纳斯县| 永定县| 麻江县| 通化市| 临澧县| 临洮县| 上栗县| 万盛区| 正宁县| 安吉县| 鄂州市| 深圳市| 手机| 庄河市| 阿拉善盟| 随州市| 浪卡子县| 温泉县| 达孜县| 会同县| 怀远县| 阜新市| 福贡县| 利川市| 巴林左旗| 宣化县| 德保县|