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