Skip to content

Instantly share code, notes, and snippets.

@hoshomoh
Last active October 6, 2022 09:29
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 hoshomoh/ea87075c9e42b7038bc3c4e342afa393 to your computer and use it in GitHub Desktop.
Save hoshomoh/ea87075c9e42b7038bc3c4e342afa393 to your computer and use it in GitHub Desktop.
Retries
const retryRequest = (config) => {
config.retry -= 1;
const delayRetryRequest = new Promise((resolve) => {
setTimeout(() => {
resolve();
}, config.retryDelay || 1000);
});
return delayRetryRequest.then(() => instance(config));
};
const instance = axios.create({
retry: 3,
retryDelay: 3000,
successRetryCondition: ({ data }) => {
console.log(data);
return data.whatever === 'pending';
},
});
instance.interceptors.response.use((response) => {
const { config, message } = err;
if (!config || !config.retry || !config.successRetryCondition(response)) {
return Promise.resolve(response);
}
return retryRequest(config);
}, undefined);
instance.get(url, {});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment