简体   繁体   中英

Handler exception in a forkjoing with Ionic 2 and Angular 4

Is it possible to handle errors separately using 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 . 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 ...
  });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM