Created
February 4, 2020 06:39
-
-
Save okmttdhr/c056e28fc63781bad0cd9e95e33625cf to your computer and use it in GitHub Desktop.
Retry promise with incremental backoff in TypeScript
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
export const retry = (fn: () => Promise<any>, timeout = 1000, retries = 5) => | |
new Promise((resolve, reject) => { | |
fn() | |
.then(resolve) | |
.catch(() => { | |
setTimeout(() => { | |
const retriesLeft = retries - 1; | |
if (retries < 0) { | |
return reject('maximum retries exceeded'); | |
} | |
retry(fn, timeout * 2, retriesLeft).then(resolve); | |
}, timeout); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://gist.github.com/briancavalier/842626