Skip to content

Instantly share code, notes, and snippets.

@hubgit
Created April 11, 2021 08:09
Show Gist options
  • Select an option

  • Save hubgit/65d674e9cd9a8a3c4c39b458efef5a0b to your computer and use it in GitHub Desktop.

Select an option

Save hubgit/65d674e9cd9a8a3c4c39b458efef5a0b to your computer and use it in GitHub Desktop.
Fetch with retries
const robustFetch = async (url, tries = 3) => {
let response
do {
try {
response = await fetch(url)
// TODO: check for response.ok
return response
} catch (error) {
console.error(error)
tries--
}
// TODO: back off
await new Promise((resolve) => window.setTimeout(resolve, 1000))
} while (tries)
throw new Error(`Failed to fetch ${url}`)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment