Last active
May 28, 2018 21:34
-
-
Save alex-okrushko/dcbfeba8e0679cf34c016ad4f40da92d to your computer and use it in GitHub Desktop.
Exponential backoff retry - example with fake service
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
// 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