I am passing some values to an rxjs pipe and then subscribing to them. If there are any error
s, I want to skip them and proceed with the remaining inputs.
of('foo', 'bar', 'error', 'bazz', 'nar', 'error', 'car').subscribe(...)
This is what I have done:
async function failIfError(item: string) {
if (item === 'error') throw new Error('HTTP 500'); // This is an error we cannot control
return item;
}
of('foo', 'bar', 'error', 'bazz', 'nar', 'error', 'car')
.pipe(
concatMap((item) => failIfError(item)),
catchError((error) => of(null))
)
.subscribe((val) => console.log('value:', val));
Stackblitz: https://stackblitz.com/edit/rxjs-pfdtrr?devToolsHeight=33&file=index.ts
This is the current output:
value: foo
value: bar
value: null
How can I modify this code so that it skips the error (returning null
) and continues with the execution? So that the output appears as follows:
value: foo
value: bar
value: null
value: bazz
value: nar
value: null
value: car