Skip to content

Instantly share code, notes, and snippets.

@euharrison
Created August 25, 2021 19:23
Show Gist options
  • Save euharrison/ce85ae3d53135e575992851ef5069fca to your computer and use it in GitHub Desktop.
Save euharrison/ce85ae3d53135e575992851ef5069fca to your computer and use it in GitHub Desktop.
Try hard a request before give up
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