Axios Retry成功响应拦截器

时间:2020-07-31 16:14:56

标签: javascript api axios interceptor axios-retry

如果在指定时间后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)
     })

2 个答案:

答案 0 :(得分:0)

问题是您没有履行内在的诺言吗?

App.render()

答案 1 :(得分:0)

return new Promise(resolve => {
                const abc = setTimeout(() => {
                    axios.request(config).then(resolve);
                    clearTimeout(abc);
                }, data.retryAfter);
            });

@TKol尝试过此方法,但没有用

相关问题