Created
June 13, 2023 17:46
-
-
Save therealparmesh/5cea48720aa329dca2c34db4c6f719e1 to your computer and use it in GitHub Desktop.
retry async
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
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