Search code examples
angularobservable

forkJoin returns and observable


I'm not sure if the title is correct for this question but i have a following scenario.

const catalog$ = this.proxy.getCatalogData(); // this is an http req
const pay$ = this.proxy.getPaydData(); // this is an http req
const acc$ = this.proxy.getAccData(); // this is an http req
const service$ = this.proxy.getServiceData(); // this is an http req and returns some error or throwError(503)

let arr = [
  catalog$,
  pay$,
  acc$,
  service$
];

arr = arr.map(item => item.pipe(catchError(err => of(err)));

forkJoin(arr).subscribe(data => {
  console.log(data); // [{...}, {...}, {...}, Observable]
});

Now even though i have forkJoin i get an observable in the callback data for the api which throwing error. Is there is way that i can resolve or get the data form the error observable inside the data array? so i dont have to process it any more.


Solution

  • You get it because you are sending one observable from catchError from the pipe:

    arr = arr.map(item => item.pipe(catchError(err => of(err)));
    // --------Here-----------------------------------^^^^^^^
    

    of() returns an observable with the args provided.

    You can just return the error

    arr = arr.map(item => item.pipe(catchError(err => err));
    

    Now you can get the error object.