[英]Handler exception in a forkjoing with Ionic 2 and Angular 4
是否可以使用forkjoin分別處理錯誤? 我需要同時調用多個請求,並在調用請求時在屏幕上顯示負載。 如果某個請求中發生錯誤,而另一個成功,則我需要在屏幕上成功顯示該請求,並將錯誤消息顯示給另一個。
沒有頁面的頁面:
getData(){
this.loadingService.showLoader("Loading, please wait");
Observable.forkJoin([
this.issuesService.getIssues(),
this.categoryService.getCaretories()
]).subscribe(
(response) => {
let issues = JSON.parse((response[0] as any)._body);
let categories = JSON.parse((response[1] as any)._body);
//do something with issues and categories
}, (error) => {
console.log(`Backend returned error was: ${error}`);
this.closeLoading(refresher);
this.showContent = false;
}, () => {
console.log('complete all request');
this.closeLoading(refresher);
});
}
}
如果在“ 問題”請求和“ 類別”請求中發生錯誤,則返回成功。 我需要在屏幕上顯示如何成功分類 ,並顯示“ 問題”的錯誤消息。
當前,發生錯誤時,將為所有請求顯示錯誤消息。
RxJS forkJoin
工作方式類似於Promise.all
。 如果其中一個來源引發錯誤,則產生的來源也會引發。 如果不應該拋出該錯誤,則應在拋出該錯誤的源中捕獲該錯誤:
Observable.forkJoin(
this.issuesService.getIssues().catch(err => Observable.of(err)),
this.categoryService.getCaretories().catch(err => Observable.of(err))
)
然后可以像常規值一樣處理錯誤:
.subscribe(([issuesRes, categoriesRes]) => {
if (issuesRes instanceof Error) ...
else ...
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.