skip to content

retry

 

retry will attempt to resubscribe to source Observable if source has failed. It's useful, for example, when retrying failed network requests.

See retryWhen operator for a more strategic approach

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
const { rxObserver, palette } = require('api/v0.3');
const { timer, from } = require('rxjs');
const { zip, map, retry } = require('rxjs/operators');

const error$ = timer(0, 5).pipe(
    map(x=>{
      if (x>2) { throw 'Bam!' }
      return x;
    }),
    zip(from(palette), Marble) // colorize the stream
  );

// retry 2 times
const retry$ = error$.pipe(
    retry(2)
  );


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


// helpers
// creates a colored Marble
function Marble(value,color) {
  return {
    valueOf: ()=>value
    , color
  };
}

0msstart00 11 22 Bam!!start00 11 22 00 11 22 00 11 22 Bam!!

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