Last active
April 5, 2019 08:33
-
-
Save kosich/2bcd4bb2ee9be92de22d65b39c5ec813 to your computer and use it in GitHub Desktop.
Limited retries on error with a retryWhen
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { rxObserver } = require('api/v0.3'); | |
const { timer, of, throwError } = require('rxjs'); | |
const { merge, map, tap, take, finalize, retryWhen, switchMap } = require('rxjs/operators'); | |
const sourceObserver = rxObserver('Source'); | |
const errorsObserver = rxObserver('Errors'); | |
const resultObserver = rxObserver('Result'); | |
timer(10, 10).pipe( | |
map(x => { | |
const time = Date.now(); | |
if (time <= 30) { | |
throw x; | |
} | |
return x; | |
}), | |
tap(sourceObserver), | |
retryWhen(errors$ => | |
errors$.pipe( | |
switchMap((error, index)=>{ | |
// on 4th error -- propagate the error | |
if (index == 3) { | |
return throwError(error); | |
} | |
// trigger another retry | |
return of(error); | |
}), | |
// Errors stream visualization | |
tap(errorsObserver), | |
finalize(errorsObserver.complete) | |
) | |
), | |
take(10) | |
) | |
.subscribe(resultObserver); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Run at https://observable-playground.github.io/gist/2bcd4bb2ee9be92de22d65b39c5ec813