skip to content

catchError

 

catchError lets you substitute an error on a stream with another stream

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
const { rxObserver } = require('api/v0.3');
const { timer, throwError, of } = require('rxjs');
const { switchMap, catchError } = require('rxjs/operators');

// emit an error in 5ms
const error$ = timer(5).pipe(
    switchMap(() => throwError('oh'))
  );


const catch$ = error$.pipe(
    catchError(err => of(err))
  );


error$.subscribe(rxObserver());
catch$.subscribe(rxObserver());

0msstartoh!startcompleteohoh

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