1. then中有錯(cuò)誤皆疹,Promise會自動(dòng)捕獲內(nèi)部異常,并交給rejected響應(yīng)函數(shù)處理占拍。
this.getData().then(res => {
const a = null
console.log(a.b) // 明顯的錯(cuò)誤會走到下面的catch
}).catch(err => {
console.log('catch err')
})
2. catch后面又寫then
vue中一般api請求放在actions中
action.js:
getData({ commit }, params = {}) {
return Api.getData(params).then((res) => {
return res
}).catch(err => {
// 如果接口出錯(cuò)走了catch
})
},
index.vue:
init() {
this.getData().then(res => {
// 如果上面走了catch略就,這邊的then也會執(zhí)行
console.log('then again')
})
}