Skip to content

Instantly share code, notes, and snippets.

@therealparmesh
Created June 13, 2023 17:46
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 therealparmesh/5cea48720aa329dca2c34db4c6f719e1 to your computer and use it in GitHub Desktop.
Save therealparmesh/5cea48720aa329dca2c34db4c6f719e1 to your computer and use it in GitHub Desktop.
retry async
export async function retryAsync<T>(
asyncFn: () => Promise<T>,
maxRetries: number,
): Promise<T> {
let retries = 0;
while (retries < maxRetries) {
try {
return await asyncFn();
} catch (error) {
retries++;
console.error(`Attempt ${retries} failed. Retrying...`);
}
}
throw new Error(`Failed after ${maxRetries} retries.`);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment