Skip to content

Instantly share code, notes, and snippets.

@alex-okrushko
Last active May 28, 2018 21:34
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 alex-okrushko/dcbfeba8e0679cf34c016ad4f40da92d to your computer and use it in GitHub Desktop.
Save alex-okrushko/dcbfeba8e0679cf34c016ad4f40da92d to your computer and use it in GitHub Desktop.
Exponential backoff retry - example with fake service
// Determine if the error matches our expected type
// http://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards
function isHttpError(error: {}): error is HttpError {
// This is a type guard for interface
// if HttpError was a class we would use instanceof check instead
return (error as HttpError).status !== undefined;
}
message$ = of('Call me!').pipe(
tap(console.log),
switchMap(() => this.service.callBackend()),
retryBackoff({
initialInterval: INIT_INTERVAL_MS,
maxInterval: MAX_INTERVAL_MS,
shouldRetry: (error) => {
// error could be anything, including HttpError that
// we want to handle from sevice.callBackend()
if (isHttpError(error)) {
// If this is HttpError and status is not 404
// then continue retrying
return error.status !== '404';
}
// should retry for the rest of the types of errors.
return true;
},
}),
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment