Last active
October 6, 2022 09:29
-
-
Save hoshomoh/ea87075c9e42b7038bc3c4e342afa393 to your computer and use it in GitHub Desktop.
Retries
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
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