Created
March 5, 2020 09:29
-
-
Save thebiltheory/699b58ab15c13ad373822e35d1ed2b6a to your computer and use it in GitHub Desktop.
This file contains hidden or 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
function waitFor(ms: number): Promise<void> { | |
return new Promise(resolve => setTimeout(resolve, ms)); | |
} | |
export default async function fetchRetry(promise: Promise<unknown>, n: number, waitNseconds = 1000): Promise<unknown> { | |
try { | |
return await promise; | |
} catch (error) { | |
if (n === 1) throw error; | |
await waitFor(waitNseconds); | |
return fetchRetry(promise, n - 1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment