Created
May 15, 2019 12:00
-
-
Save helloris25/2836547396a576e25c8cd4e265543a6f to your computer and use it in GitHub Desktop.
Retry async function when was reject
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
async function retry(fn, retriesLeft = 5, interval = 1000, exponential = false) { | |
try { | |
const val = await fn(); | |
return val; | |
} catch (error) { | |
if (retriesLeft) { | |
await new Promise(r => setTimeout(r, interval)); | |
return retry(fn, retriesLeft - 1, exponential ? interval * 2 : interval, exponential); | |
} else throw new Error('Max retries reached'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment