skip to content

onErrorResumeNext

 

Switch to the next observable when current errors.
This helps you build a fallback strategy:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
const { rxObserver } = require('api/v0.3');
const { timer, onErrorResumeNext, throwError, of } = require('rxjs');
const { concat, ignoreElements } = require('rxjs/operators');


const failTimer$ = timer(5).pipe(
    ignoreElements(),
    concat(throwError('Ouch!'))
  );

const fineTimer$ = timer(5).pipe(
    ignoreElements(),
    concat(of('Ok!'))
  );

const strategicTimer$ = onErrorResumeNext(
    failTimer$,
    fineTimer$
  );

failTimer$.subscribe(rxObserver('fail timer'));
fineTimer$.subscribe(rxObserver('fine timer'));
strategicTimer$.subscribe(rxObserver('strategic'));

0msfail timerstartOuch!!fine timerstartcompleteOk!Ok! strategicstartcompleteOk!Ok!

Check out "Error handling in RxJS" article to get better understanding how not to fail with Observables.