简体   繁体   中英

how to combine observables to one ignoring errors in rxjs

I have several observables and want to combine them to one but want to ignore those which emits errors, I want the operator to emit values for successful observables even if some of them emits an error.

I was looking for operators like concat, forkJoin but when an error occurs they emit it immediately

import { concat, interval } from 'rxjs';
import { take } from 'rxjs/operators';

const timer1 = interval(1000).pipe(take(10));
const timer2 = interval(2000).pipe(take(6));
const timer3 = interval(500).pipe(take(10));

const result = concat(timer1, timer2, timer3);
result.subscribe(x => console.log(x));

// results in the following:
// (Prints to console sequentially)
// -1000ms-> 0 -1000ms-> 1 -1000ms-> ... 9
// -2000ms-> 0 -2000ms-> 1 -2000ms-> ... 5
// -500ms-> 0 -500ms-> 1 -500ms-> ... 9

Error notifications always propagate to the top. You have to handle errors on those inner Observables with catchError and return an Observable that doesn't error. eg

const inner1 = interval(1000).pipe(take(10), catchError(error => EMPTY));
const inner2 = interval(2000).pipe(take(5), catchError(error => of(null)));

const outer = merge(inner1, inner2)

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