如果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)
})发布于 2020-08-01 00:19:34
问题是你没有解决你内心的承诺吗?
function axiosRetrySuccessInterceptor(response) {
const { status, data, config } = response;
// Retry the request on http status code 202
if (status === 202 ) {
return new Promise((resolve) => {
setTimeout(() => {
axios.request(config).then(resolve);
}, data.retryAfter);
});
} else {
// No need to do anything, take rest
return Promise.resolve(response);
}
}发布于 2020-08-01 00:40:14
return new Promise(resolve => {
const abc = setTimeout(() => {
axios.request(config).then(resolve);
clearTimeout(abc);
}, data.retryAfter);
});@TKol已经尝试过了,但不起作用
https://stackoverflow.com/questions/63195488
复制相似问题