Skip to content

Instantly share code, notes, and snippets.

@kosich
Last active April 5, 2019 08:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kosich/2bcd4bb2ee9be92de22d65b39c5ec813 to your computer and use it in GitHub Desktop.
Save kosich/2bcd4bb2ee9be92de22d65b39c5ec813 to your computer and use it in GitHub Desktop.
Limited retries on error with a retryWhen
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);
@kosich
Copy link
Author

kosich commented Apr 5, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment