Last active
June 22, 2023 14:48
-
-
Save tolotrasmile/bd2b8fd8701a48db3f77a841bc1a1893 to your computer and use it in GitHub Desktop.
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
type FetchParams = Parameters<typeof fetch> | |
type FetchReturn = ReturnType<typeof fetch> | |
export function fetchRetry( | |
input: FetchParams[0], | |
init?: FetchParams[1], | |
retry: number = 0, | |
delayMs: number = 1000 | |
): FetchReturn { | |
return fetch(input, init).catch(error => { | |
const triesLeft = retry - 1 | |
if (triesLeft <= 0) { | |
throw error | |
} | |
// Await for delayMs before trying another attempt | |
return new Promise(resolve => setTimeout(resolve, delayMs)).then(() => | |
fetchRetry(input, init, triesLeft, delayMs) | |
) | |
}) | |
} |
Author
tolotrasmile
commented
Jun 22, 2023
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment