Skip to content

Latest commit

 

History

History
50 lines (40 loc) · 1.39 KB

retry.md

File metadata and controls

50 lines (40 loc) · 1.39 KB

retry

signature: retry(number: number): Observable

Retry an observable sequence a specific number of times should an error occur.

Examples

Example 1: Retry 2 times on error

( jsBin | jsFiddle )

//emit value every 1s
const source = Rx.Observable.interval(1000);
const example = source
  .flatMap(val => {
    //throw error for demonstration
    if (val > 5) {
      return Rx.Observable.throw('Error!');
    }
    return Rx.Observable.of(val);
  })
  //retry 2 times on error
  .retry(2);
/*
  output: 
  0..1..2..3..4..5..
  0..1..2..3..4..5..
  0..1..2..3..4..5..
  "Error!: Retried 2 times then quit!"
*/
const subscribe = example.subscribe({
  next: val => console.log(val),
  error: val => console.log(`${val}: Retried 2 times then quit!`)
});

Additional Resources


📁 Source Code: https://github.com/ReactiveX/rxjs/blob/master/src/operator/retry.ts