Skip to content

Instantly share code, notes, and snippets.

@thebiltheory
Created March 5, 2020 09:29
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 thebiltheory/699b58ab15c13ad373822e35d1ed2b6a to your computer and use it in GitHub Desktop.
Save thebiltheory/699b58ab15c13ad373822e35d1ed2b6a to your computer and use it in GitHub Desktop.
function waitFor(ms: number): Promise<void> {
return new Promise(resolve => setTimeout(resolve, ms));
}
export default async function fetchRetry(promise: Promise<unknown>, n: number, waitNseconds = 1000): Promise<unknown> {
try {
return await promise;
} catch (error) {
if (n === 1) throw error;
await waitFor(waitNseconds);
return fetchRetry(promise, n - 1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment