Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@sethdavis512
Last active August 4, 2022 06:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sethdavis512/897edb7937aab533013974965ae37969 to your computer and use it in GitHub Desktop.
Save sethdavis512/897edb7937aab533013974965ae37969 to your computer and use it in GitHub Desktop.
const pause = (duration) => new Promise(res => setTimeout(res, duration));
const retry = (retries, fn) =>
fn().catch(err => retries > 1 ? retry(retries - 1, fn) : Promise.reject(err));
const backoff = (retries, fn, delay = 500) =>
fn().catch(err => retries > 1
? pause(delay).then(() => backoff(retries - 1, fn, delay * 2))
: Promise.reject(err));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment