Skip to content

Instantly share code, notes, and snippets.

@hoshomoh
Last active October 6, 2022 09:29
Show Gist options
  • 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