Skip to content

Instantly share code, notes, and snippets.

@VelinGeorgiev
Created March 25, 2021 17:15
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 VelinGeorgiev/fb131f6f3eb4de010a76187e9a86384f to your computer and use it in GitHub Desktop.
Save VelinGeorgiev/fb131f6f3eb4de010a76187e9a86384f to your computer and use it in GitHub Desktop.
const fetchRetry = async (url, opts) {
let retryFor = 4
let error = "Something went wrong"
const f = async () => {
try {
const resp = await fetch(url, opts)
return [,resp.json()]
} catch (e) {
return [e]
}
}
while(--retryFor) { // 3 times retry
const [err, res] = f()
if(res) return res
console.log('Error')
console.log(e)
console.log('Will retry in 30 sec')
error = err
await new Promise(resolve => setTimeout(resolve, 30000)) //sleep for 30 sec
}
throw new Error(error)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment