Skip to content

Instantly share code, notes, and snippets.

@jakubbarczyk
Last active December 25, 2023 13:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jakubbarczyk/d66692edbf54a26cd73307fbb957aa45 to your computer and use it in GitHub Desktop.
Save jakubbarczyk/d66692edbf54a26cd73307fbb957aa45 to your computer and use it in GitHub Desktop.
const fetchWithRetries = async (url, options, retryCount = 0) => {
const { maxRetries = 3, ...remainingOptions } = options;
try {
return await fetch(url, remainingOptions);
} catch (error) {
if (retryCount < maxRetries) {
return fetchWithRetries(url, options, retryCount + 1);
}
throw Error('Max retries exceeded', { cause: error });
}
};
export fetchWithRetries;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment