Skip to content

Instantly share code, notes, and snippets.

@ilyas-shah
Created November 6, 2018 19:05
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 ilyas-shah/3db2dfda86390cc9a3cc260870ec214c to your computer and use it in GitHub Desktop.
Save ilyas-shah/3db2dfda86390cc9a3cc260870ec214c to your computer and use it in GitHub Desktop.
retrying a request after some timeout
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