Created
April 22, 2019 06:22
-
-
Save SafeerH/b49f409e458918d74dc2f9906f7d9284 to your computer and use it in GitHub Desktop.
TypeScript retry async helper
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
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