Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Axios Interceptor to delay and retry original request for certain status code (polling)
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