【发布时间】:2020-11-21 12:23:17
【问题描述】:
如果 API 响应的状态码在指定时间后为 202,我想重试请求。
在 axios 成功拦截器中,我是这样做的
function axiosRetrySuccessInterceptor(response) {
const { status, config } = response;
// Retry the request on http status code 202
if (status === 202 ) {
return new Promise(() => {
setTimeout(() => {
axios.request(config)
}, 2000);
});
} else {
// No need to do anything, take rest
return Promise.resolve(response);
}
}
// calling function
axios.interceptors.response.use(axiosRetrySuccessInterceptor,axiosRetryInterceptor);
如果有其他非 202 API 调用,我想清除之前的重试请求。
我也添加了 clearTimeout 但没有成功。
当我返回上一个屏幕时,会调用新的 API,但仍会调用以前的重试 API。它不会停止。 也使用 clearTimeout 尝试过类似下面的操作,但没有成功,而是停止重试
var retry = setTimeout(() => {
axios.request(config)
clearTimeout(retry)
})
【问题讨论】:
标签: javascript api axios interceptor axios-retry