Skip to content

Instantly share code, notes, and snippets.

@helloris25
Created May 15, 2019 12:00
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 helloris25/2836547396a576e25c8cd4e265543a6f to your computer and use it in GitHub Desktop.
Save helloris25/2836547396a576e25c8cd4e265543a6f to your computer and use it in GitHub Desktop.
Retry async function when was reject
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