Last active
January 12, 2019 22:49
-
-
Save AaronNGray/85a31df95166a4ab35b3d376b119a2a3 to your computer and use it in GitHub Desktop.
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
// code from https://tech.mybuilder.com/handling-retries-and-back-off-attempts-with-javascript-promises/ | |
const pause = (duration) => new Promise(res => setTimeout(res, duration)); | |
const retry = (self, fn, args, retries, delay = 500, factor = 2) => | |
fn.apply(self, args).catch(err => retries > 1 | |
? pause(delay).then(() => retry(self, fn, args, retries - 1, delay * factor, factor)) | |
: Promise.reject(err)); | |
module.exports = retry; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment