Created
March 25, 2021 17:15
-
-
Save VelinGeorgiev/fb131f6f3eb4de010a76187e9a86384f to your computer and use it in GitHub Desktop.
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
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