New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can I cancel promise in response interceptor? #583

Closed
foxinmy opened this Issue Dec 8, 2016 · 3 comments

Comments

Projects
None yet
4 participants
@foxinmy

foxinmy commented Dec 8, 2016

  var source = axios.CancelToken.source();
    axios.interceptors.request.use(function (config) {
      config.cancelToken =  source.token;
      return config;
    }, function (error) {
      return Promise.reject(error);
    });
    axios.interceptors.response.use(function (response) {
        source.cancel('Operation canceled by the user.');
        return response;
    }, function (error) {
        return Promise.reject(error);
    });

This approach seems not working。

THX。(:

@foxinmy foxinmy changed the title from Can I cancel promise in response interceptor to Can I cancel promise in response interceptor? Dec 8, 2016

@mzabriskie

This comment has been minimized.

Member

mzabriskie commented Dec 8, 2016

@nickuraltsev wrote the implementation for cancelation. Maybe he can answer?

@nickuraltsev

This comment has been minimized.

Member

nickuraltsev commented Dec 9, 2016

You cannot use the token in a response interceptor, but you can throw a Cancel instead:

  axios.interceptors.response.use(function (response) {
    throw new axios.Cancel('Operation canceled by the user.');
  }, function (error) {
    return Promise.reject(error);
  });

Hope this helps!

@JounQin

This comment has been minimized.

JounQin commented Dec 15, 2016

@nickuraltsev What I if want to handle the error but do not reject it further?

axios.interceptors.response.use(response => response, error => {
   const response = error.response;
   if(response.status === 404) {
       // how to cancel the Promise here?
       return false;
   }
    return Promise.reject(error);
  });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment