I have array of Observable, Used RxJS forkJoin to capture the results emitted by each of the Observable in the array. But if any of the observable fails to emit data, the forkJoin emits no data. So I am trying for similar behavior like forkJoin with error handling. Please do suggest any alternative that can solve the problem described above.

For work around I have created for loop for array of observable and doing error handling for each of Observable subscription. however with help of Arrow function i am able to preserve the order of the result emitted by each observable.

// Sample code block
const arrayOfObservable = [t,t,t,t,t];

arrayOfObservable.forEach((item, index) => {
    item.subscribe((result) => {
         // Success
    },
    (error) => {
         // Error
    },
    () => {
         // Completed
    });
});

1 Answers

1
saurabh On

You can use forkJoin just make sure to use catchError on inner observable so that even if an error occurs it emits all the data along with the error data.

example:

import { ajax } from 'rxjs/ajax';
import { catchError } from 'rxjs/operators';
import { forkJoin, of, throwError } from 'rxjs';

forkJoin(
    ajax.getJSON('https://api.github.com/users/google'),
    ajax.getJSON('https://api.github.com/users/microsoft'),
    /* this throws an error */
    ajax.getJSON('https://api.github.com/uses').pipe(catchError(error => of(error))),
)
// [object,  object,  Error ]
.subscribe(console.log);

link to the working example of the same : https://stackblitz.com/edit/typescript-czentk