[英]Breaking out of then promise chain
如何突破Promise鏈(處理錯誤並停止運行其他.then)?
我的代碼:
onSubmit(f: any) {
let loading = this.loadingCtrl.create({
content: 'Sending a form...'
});
loading.present();
this.submitProvider.sendByPost(f)
.then(
(res) => { this.formSent = true, console.log('Form is sent') },
(err) => { console.log('Encountered an error...')
//error here, break out and deal with error
}
)
.then( (res) => {
loading.dismiss();
this.pushPage();
this.removeChosenPicture();
this.removeTakenPicture();
this.myForm.reset({ selectedDate: this.selectedDate }) }
)
}
您可以使用catch()
方法:通過這樣做,您將處理promise鏈中的所有錯誤。 當錯誤在鏈中的任何地方拋出時,它將“脫離” promise鏈並最終以catch方法結束。 MDN:.catch()方法
onSubmit(f: any) {
let loading = this.loadingCtrl.create({
content: 'Sending a form...'
});
loading.present();
this.submitProvider.sendByPost(f)
.then(
(res) => { this.formSent = true, console.log('Form is sent') }
)
.then( (res) => {
loading.dismiss();
this.pushPage();
this.removeChosenPicture();
this.removeTakenPicture();
this.myForm.reset({ selectedDate: this.selectedDate }) }
).catch((err) =>{ /*Handle your errors*/})
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.