skip to content

retryWhen

 

retryWhen let's you do basic retry with strategy, based on error emissions:

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
26
27
28
29
30
31
32
33
const {rxObserver} = require('api/v0.3');
const { timer } = require('rxjs');
const { map, tap, retryWhen, delayWhen } = require('rxjs/operators');

const source$ =
  timer(0, 100).pipe(
    map(val => {
      if (val == 1) {
        throw 'Err';
      }
      return val;
    })
  );

const result$ = source$.pipe(
    retryWhen(errors =>
      // here Errors are just events
      errors.pipe(
        // show error messages thread
        tap(rxObserver('Error messages')),
        // will restart with increasing delay
        delayWhen((_, index) => timer(index * 50))
      )
    )
);

source$.subscribe(rxObserver('source$'));
result$.subscribe(rxObserver('result$'));

// a modification of
// https://www.learnrxjs.io/operators/error_handling/retrywhen.html

⚠️ Execution time is limited to 1000ms
0mssource$start00 Err!result$start00 00 00 00 00 00 Error messagesstartErrErr ErrErr ErrErr ErrErr ErrErr

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