Skip to content

Instantly share code, notes, and snippets.

@pettomartino
Last active March 12, 2019 18:14
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 pettomartino/2fc131211dcbe444a2940bd9baaed688 to your computer and use it in GitHub Desktop.
Save pettomartino/2fc131211dcbe444a2940bd9baaed688 to your computer and use it in GitHub Desktop.
Retry promise
const retry = (fn, options = {}) => new Promise((resolve, reject) => {
const { retryInterval = 500, retries = 1 } = options;
fn()
.then(resolve)
.catch(err => (
(retries === 0)
? reject(err)
: setTimeout(
() => retry(fn, { retryInterval, retries: retries - 1 }).then(resolve, reject),
retryInterval,
)
));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment