Created
November 6, 2018 18:39
-
-
Save holmberd/b23e18c15827db43af1383b4e41fb0c1 to your computer and use it in GitHub Desktop.
Javascript Recursive Asynchronous Retry Function
This file contains hidden or 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
/** | |
* 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