Created
September 17, 2022 03:01
-
-
Save semlinker/842faec4a18a05fc92619d8ec02d3475 to your computer and use it in GitHub Desktop.
axios request retry
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
axios.interceptors.response.use(null, (err) => { | |
let config = err.config; | |
if (!config || !config.retryTimes) return Promise.reject(err); | |
const { __retryCount = 0, retryDelay = 300, retryTimes } = config; | |
config.__retryCount = __retryCount; | |
// Check whether the number of retries has been exceeded | |
if (__retryCount >= retryTimes) { | |
return Promise.reject(err); | |
} | |
// Increase the number of retries | |
config.__retryCount++; | |
const delay = new Promise((resolve) => { | |
setTimeout(() => { | |
resolve(); | |
}, retryDelay); | |
}); | |
// Resend | |
return delay.then(function () { | |
return axios(config); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment