Last active
February 24, 2018 22:19
-
-
Save anishpateluk/9ed888b30fdfeb1cc6bdd358bf2256cb to your computer and use it in GitHub Desktop.
basic js retry circuit breaker for async functions
This file contains 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
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