Created
July 10, 2017 07:09
-
-
Save andregoncalves/bd9c648a5586ae0c8c132f25d30ddb88 to your computer and use it in GitHub Desktop.
HTTP Request Retry with exponential backoff with Async
This file contains hidden or 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