简体   繁体   English

Promise.reject(error) 和 Axios 中的返回错误有什么区别?

[英]What's difference between Promise.reject(error) and return error in Axios?

I have a general query about Axios and Promises.我有一个关于 Axios 和 Promises 的一般查询。 Maybe someone can help me clear this concept.也许有人可以帮助我清除这个概念。

Basic Interceptor of Axios includes: Axios的基本拦截器包括:

// Add a response interceptor
axios.interceptors.response.use(function (response) {
    // Any status code that lie within the range of 2xx cause this function to trigger
    // Do something with response data
    return response;
  }, function (error) {
    // Any status codes that falls outside the range of 2xx cause this function to trigger
    // Do something with response error
    return Promise.reject(error);
  });

My question is what's the difference between returning error as我的问题是返回错误有什么区别

Project.reject(error);

and as并作为

return error;

This response and error goes to the Request 'then' amd 'catch' blocks respectively I believe.我相信,此响应和错误分别转到 Request 'then' amd 'catch' 块。 But what's the difference if we use Promise.resolve and Promise.reject?但是如果我们使用 Promise.resolve 和 Promise.reject 有什么区别呢?

I just tried (it's that easy ) and the difference is that by returning a rejected promise ( return Promise.reject(error) ), you tell axios that the response has an error condition and that the upstream code should be notified about it ( axios(…).catch(…) will trigger).我刚刚尝试过(就这么简单),不同之处在于通过返回被拒绝的 promise(返回axios(…).catch(…) return Promise.reject(error) ),您告诉axios axios(…).catch(…)将触发)。

When returning just the error ( return error ), you're telling axios to basically ignore the error and the upstream code will not know about it ( axios(…).then(…) will trigger).当只返回错误( return error )时,您是在告诉axios基本上忽略该错误并且上游代码不会知道它( axios(…).then(…)将触发)。

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

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