Skip to content

Instantly share code, notes, and snippets.

@sierranevadan
sierranevadan / retryPromise.js
Created October 2, 2017 17:51
retry promise from factory function with exponential backoff
/*
* Retry a Promise until success or max tries exceeded.
*
* @param factory {Function} A function that generates the Promise to try.
* @param validator {Function} A function that resolves if the result from `factory` is valid,
* and throws an error if the result is invalid.
* @param tries {Integer} The max number of attempts.
* @param delay {Integer} The first amount of time to wait between attempts, in milliseconds.
* Subsequent attempts will wait exponentially more time.
* @returns {Promise} A Promise that resolves to the result of `factory`.
### Keybase proof
I hereby claim:
* I am sierranevadan on github.
* I am grantkelly (https://keybase.io/grantkelly) on keybase.
* I have a public key whose fingerprint is E26B 4DEC 404E 44FC 60A1 FA39 A0BB C5B6 EA86 785E
To claim this, I am signing this object: