Last active
July 16, 2017 01:17
-
-
Save groz/8182e16cf0f1fb3c7552218be1d5874b to your computer and use it in GitHub Desktop.
Retries with backoff in RxJS
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 backoff = (maxRetries, delayFunction) => | |
attempts => attempts | |
.zip(Rx.Observable.range(1, maxRetries), (a, idx) => idx) | |
.delayWhen(idx => Rx.Observable.timer(delayFunction(idx))); | |
// usage | |
const withIncrementalBackoff = observable.retryWhen(backoff(3, nRetry => nRetry * 1000)); | |
const withExponentialBackoff = observable.retryWhen(backoff(3, nRetry => nRetry * nRetry * 1000)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The backoff retry here works, but if the final retry fails the observable still completes. Maybe more natural to fail the observable if the final retry fails.