Skip to content

Instantly share code, notes, and snippets.

@holmberd
Created November 6, 2018 18:39
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 holmberd/b23e18c15827db43af1383b4e41fb0c1 to your computer and use it in GitHub Desktop.
Save holmberd/b23e18c15827db43af1383b4e41fb0c1 to your computer and use it in GitHub Desktop.
Javascript Recursive Asynchronous Retry Function
/**
* Retries a async function recursively n times.
*
* @param {function} fn
* @param {Number} [retries=3]
* @returns {Promise}
*/
function retry(fn, retries=3, err=null) {
if (retries === 0) {
return Promise.reject(err);
}
return fn().catch(err => {
return retry(fn, (retries - 1), err);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment