首页 > 解决方案 > Axios - 拦截器:防止原始调用者进一步处理

问题描述

我的 Axios 实例有一个成功的响应拦截器。在某些情况下,我基本上想阻止.then()'s 和.catch()'s 的进一步执行。

axiosExtended.interceptors.response.use(response => successHandler(response), error => errorHandler(error))

function successHandler(response) {
    const relativeRequestUrl = response.config.url;
    const relativeResponseUrl = trim(response.request.responseURL.replace(response.config.baseURL, ''), '/');

    if (relativeRequestUrl !== relativeResponseUrl) {
        // dont return anything to original caller. No then. No catch.
        router.push(relativeResponseUrl)
    }

    // get handled by original caller...
}

标签: javascriptif-statementpromiseaxiosxmlhttprequest

解决方案


感谢@felixmosh:

instance.interceptors.response.use((response) => {
  if (someCondition(response) {
    return new Promise(() => {});
  }
  return response;
});

推荐阅读