Skip to content

Instantly share code, notes, and snippets.

@andregoncalves
Created July 10, 2017 07:09
Show Gist options
  • Save andregoncalves/bd9c648a5586ae0c8c132f25d30ddb88 to your computer and use it in GitHub Desktop.
Save andregoncalves/bd9c648a5586ae0c8c132f25d30ddb88 to your computer and use it in GitHub Desktop.
HTTP Request Retry with exponential backoff with Async
function wait (timeout) {
return new Promise((resolve) => {
setTimeout(() => {
resolve()
}, timeout)
})
}
async function requestWithRetry (url) {
const MAX_RETRIES = 10
for (let i = 0; i <= MAX_RETRIES; i++) {
try {
return await request(url)
} catch (err) {
const timeout = Math.pow(2, i)
console.log('Waiting', timeout, 'ms')
await wait(timeout)
console.log('Retrying', err.message, i)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment