Skip to content

Instantly share code, notes, and snippets.

@AaronNGray
Last active January 12, 2019 22:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AaronNGray/85a31df95166a4ab35b3d376b119a2a3 to your computer and use it in GitHub Desktop.
Save AaronNGray/85a31df95166a4ab35b3d376b119a2a3 to your computer and use it in GitHub Desktop.
// 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