angular2 rxjs observable forkjoin

前端 未结 2 886
遥遥无期
遥遥无期 2020-12-03 05:23

Is it possible to continue forkjoin http.get requests even if one of the requests fails.

I\'m looking to a similar function of $q.allSettled in angular2.

Se

2条回答
  •  醉酒成梦
    2020-12-03 05:57

    Uses Observable.forkJoin() to run multiple concurrent http.get() requests. The entire operation will result in an error state if any single request fails.

     getBooksAndMovies() {
        return Observable.forkJoin(
          this.http.get('/app/books.json').map((res:Response) => res.json()),
          this.http.get('/app/movies.json').map((res:Response) => res.json())
        );
    

    But you could put your additional GET request in the error handler:

    getBooksAndMovies() {
        Observable.forkJoin(
            this.http.get('/app/books.json').map((res:Response) => res.json()),
            this.http.get('/app/movies.json').map((res:Response) => res.json())
        ).subscribe(
          data => {
            this.books = data[0]
            this.movies = data[1]
          },
          err => console.error(err)
        );
    

提交回复
热议问题