Skip to content

Instantly share code, notes, and snippets.

@SafeerH
Created April 22, 2019 06:22
Show Gist options
  • Save SafeerH/b49f409e458918d74dc2f9906f7d9284 to your computer and use it in GitHub Desktop.
Save SafeerH/b49f409e458918d74dc2f9906f7d9284 to your computer and use it in GitHub Desktop.
TypeScript retry async helper
async retryAsync<T>(fn: () => Promise<T>, retryCount: number = 3, retryInterval: number = 1000): Promise<T> {
let retries = -1;
do {
try {
retries++;
return await fn();
} catch (error) {
if (retries === retryCount) {
throw error;
}
await new Promise<void>((resolve: () => void) => setTimeout(resolve, retryInterval));
}
} while (retries < retryCount);
return fn(); // unreachable code.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment