Axios Interceptor to delay and retry original request for certain status code (polling)
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
import axios from 'axios'; | |
const instance = axios.create({ | |
baseURL: 'http://localhost:5000' | |
}); | |
const sleepRequest = (milliseconds, originalRequest) => { | |
return new Promise((resolve, reject) => { | |
setTimeout(() => resolve(instance(originalRequest)), milliseconds); | |
}); | |
}; | |
instance.interceptors.response.use(response => { | |
return response; | |
}, error => { | |
const { config, response: { status }} = error; | |
const originalRequest = config; | |
if (status === 420) { | |
return sleepRequest(1000, originalRequest); | |
} else { | |
return Promise.reject(error); | |
} | |
}); | |
export default instance; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment