简体   繁体   中英

How to ignore callback when im in interceptors in axios?

I use axios interceptors to get the error. in some cases that I have error I do not want to return to the catch or then callback.

How I do it in axios? because if I dont return value it go to the next , but if I return Promise.reject it go to the catch .

I want when I have error in interceptors, not continue forward to the callback.

codesandbox.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();

you can return a resolve promises in the error function

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

    return Promise.resolve();
  }
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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