Skip to content

Instantly share code, notes, and snippets.

@groz
Last active July 16, 2017 01:17
Show Gist options
  • Save groz/8182e16cf0f1fb3c7552218be1d5874b to your computer and use it in GitHub Desktop.
Save groz/8182e16cf0f1fb3c7552218be1d5874b to your computer and use it in GitHub Desktop.
Retries with backoff in RxJS
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));
@josephfo
Copy link

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.

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