Skip to content

Instantly share code, notes, and snippets.

@simenbrekken
Created October 12, 2016 13:10
Show Gist options
  • Save simenbrekken/22cabba27cbdfe3a0512f39750d1c648 to your computer and use it in GitHub Desktop.
Save simenbrekken/22cabba27cbdfe3a0512f39750d1c648 to your computer and use it in GitHub Desktop.
Async/await exponential retry
const startTime = Date.now()
const start = async (attempt, { retries = 3, interval = 250 }) => {
for (let tries = 0, delay = interval; tries < retries; tries++, delay *= 2) {
console.log('Waiting', delay, Date.now() - startTime)
await attempt()
console.log('Retrying!')
}
console.log('Done trying')
}
const fakeFetch = () => {
console.log('Fetching something fake')
return new Promise(resolve => setTimeout(resolve, 50))
}
start(fakeFetch, { retries: 3, interval: 250 })
@simenbrekken
Copy link
Author

@einarlove
Copy link

should have timeout support to

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment