Skip to content

Instantly share code, notes, and snippets.

@anishpateluk
Last active February 24, 2018 22:19
Show Gist options
  • Save anishpateluk/9ed888b30fdfeb1cc6bdd358bf2256cb to your computer and use it in GitHub Desktop.
Save anishpateluk/9ed888b30fdfeb1cc6bdd358bf2256cb to your computer and use it in GitHub Desktop.
basic js retry circuit breaker for async functions
function retryCircuitBreaker(attempts, limit, wait, fn) {
return new Promise((resolve, reject) => {
setTimeout(function() {
fn().then(resolve).catch(reject);
}, attempts * wait);
}).catch(function(error) {
console.log(error);
if(attempts > limit){
return Promise.reject(error);
}
return executeWithRetry(++attempts, limit, fn);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment