Created
August 25, 2021 19:23
-
-
Save euharrison/ce85ae3d53135e575992851ef5069fca to your computer and use it in GitHub Desktop.
Try hard a request before give up
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 function tryHard<T>(fn: () => Promise<T>, attempts = 3): Promise<T> { | |
try { | |
return await fn() | |
} catch (error) { | |
if (attempts > 0) { | |
return await tryHard(fn, attempts - 1) | |
} else { | |
throw error | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment