[英]Handler exception in a forkjoing with Ionic 2 and Angular 4
Is it possible to handle errors separately using forkjoin ? 是否可以使用forkjoin分别处理错误? I need to call multiple requests at the same time, displaying a loading on the screen while the requests are called.
我需要同时调用多个请求,并在调用请求时在屏幕上显示负载。 If an error occurs in some request, and another success, I need to display the request successfully on the screen, and the error message to the other.
如果某个请求中发生错误,而另一个成功,则我需要在屏幕上成功显示该请求,并将错误消息显示给另一个。
Meu código no Page.ts: 没有页面的页面:
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);
});
}
}
If an error occurs in the Issues request, and in the Category request, return success. 如果在“ 问题”请求和“ 类别”请求中发生错误,则返回成功。 I need to display on the screen how to successfully categories , and display an error message for Issues .
我需要在屏幕上显示如何成功分类 ,并显示“ 问题”的错误消息。
Currently, when an error occurs, the error message is displayed for all requests. 当前,发生错误时,将为所有请求显示错误消息。
RxJS forkJoin
works similarly to Promise.all
. RxJS
forkJoin
工作方式类似于Promise.all
。 If one of the sources throws an error, the resulting source also throws. 如果其中一个来源引发错误,则产生的来源也会引发。 If it shouldn't throw, the error should be caught in a source where it is thrown:
如果不应该抛出该错误,则应在抛出该错误的源中捕获该错误:
Observable.forkJoin(
this.issuesService.getIssues().catch(err => Observable.of(err)),
this.categoryService.getCaretories().catch(err => Observable.of(err))
)
Then errors can be handled like regular values: 然后可以像常规值一样处理错误:
.subscribe(([issuesRes, categoriesRes]) => {
if (issuesRes instanceof Error) ...
else ...
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.