繁体   English   中英

当我在 axios 的拦截器中时如何忽略回调?

[英]How to ignore callback when im in interceptors in axios?

我使用 axios 拦截器来获取错误。 在某些情况下,我有错误,我不想返回到捕获或回调。

我如何在 axios 中做到这一点? 因为如果我不返回值它会转到next ,但是如果我返回Promise.reject它会转到catch

我希望当我在拦截器中有错误时,不要继续前进到回调。

代码和盒子.io

import axios from "axios";

axios.interceptors.response.use(
  (response) => response,
  (error) => {
    console.log({ error });

    // do not return to the catch/then in foo please! <-- HOW?

    return Promise.reject(error);
  }
);

const foo = () => {
  axios
    .post("https://httpstat.us/500")
    .then((r) => {
      console.log({ r });
    })
    .catch((err) => {
      console.log({ err });
    });
};

foo();

您可以在错误函数中返回一个解决承诺

axios.interceptors.response.use(
  (response) => response,
  (error) => {
    console.log({ error });

    return Promise.resolve();
  }
);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM