I am a bit confused about what the statement
我对这句话有点困惑
return $q.reject(response);
does inside the responseError
interceptor.
在responseError拦截器内部。
I have gone through this article on webdeveasy and one on angular docs but they haven't helped.
我已经阅读了关于webdeveasy的文章和关于角度文档的文章,但是他们没有帮助。
Here is my code (for reference):
这是我的代码(供参考):
(function () {
"use strict";
angular.module('xyz').factory('errorResponseInterceptor', ['$q', 'toaster', function ($q, toaster) {
return {
...
...
...
responseError: function (response) {
...
...
//some logic here
...
...
if (response.status == 500) {
toaster.error({ title: "", body: response.statusText });
return $q.reject(response);//what does this statement does and why do we need to put it here?
}
return response;
}
};
}]);
}());
My Question is:
我的问题是:
- Why do we need to write
return $q.reject(response)
? - 为什么我们需要写回$ q.reject(响应)?
- How does that line affect the angular app (what does it do)?
- 该行如何影响角度应用程序(它做什么)?
1 个解决方案
#1
4
The $q
object in Angular allows us to define a promise and handle the behaviour associated with a promise. An example of how we might do this is:
Angular中的$ q对象允许我们定义一个promise并处理与promise相关的行为。我们如何做到这一点的一个例子是:
var q = $q.defer()
//do something
if(success){
q.resolve()
} else {
q.reject()
}
return q
What we are doing here is defining a promise object and assigning it to the variable q
. Then we perform some operation and use the result of that to determine whether the promise returns successfully or not. Calling .resolve()
on the promise object is indicative of the fact that the promise has returned correctly. By the same token calling .reject()
on the promise is indicative of the fact that is has failed.
我们在这里做的是定义一个promise对象并将其赋值给变量q。然后我们执行一些操作并使用它的结果来确定promise是否成功返回。在promise对象上调用.resolve()表示promise已正确返回。同样地,在promise上调用.reject()表示事实已经失败。
If we consider how we actually use a promise:
如果我们考虑如何实际使用承诺:
SomeFactory.getSomething().then(function(data){
//gets called if promise resolves
}, function(error){
//gets called if promise rejected
}
So in the example you have provided, we are checking to see if the response has a 500 error code, and if it does, we are rejecting the promise thus allowing us to handle the error.
因此,在您提供的示例中,我们正在检查响应是否有500错误代码,如果有,我们拒绝承诺,从而允许我们处理错误。
In a responseError
interceptor you have two choices with regards to what you return. If you return $q.reject(response)
you are continuing the error handling chain of events. If you return anything else (generally a new promise or a response) you are indicating that the error has been recovered from and should be treated as a success.
在responseError拦截器中,您有两个关于返回内容的选择。如果返回$ q.reject(响应),则表示您正在继续执行错误处理事件链。如果您返回任何其他内容(通常是新的承诺或响应),则表明错误已从中恢复,应视为成功。
With regards to your two points:
关于你的两点:
1- You need to write that line to indicate that the response should still be considered an error.
1-您需要编写该行以指示响应仍应被视为错误。
2- The effect on the angular app is that the error callback will be called instead of the success callback.
2-对角度应用程序的影响是将调用错误回调而不是成功回调。
#1
4
The $q
object in Angular allows us to define a promise and handle the behaviour associated with a promise. An example of how we might do this is:
Angular中的$ q对象允许我们定义一个promise并处理与promise相关的行为。我们如何做到这一点的一个例子是:
var q = $q.defer()
//do something
if(success){
q.resolve()
} else {
q.reject()
}
return q
What we are doing here is defining a promise object and assigning it to the variable q
. Then we perform some operation and use the result of that to determine whether the promise returns successfully or not. Calling .resolve()
on the promise object is indicative of the fact that the promise has returned correctly. By the same token calling .reject()
on the promise is indicative of the fact that is has failed.
我们在这里做的是定义一个promise对象并将其赋值给变量q。然后我们执行一些操作并使用它的结果来确定promise是否成功返回。在promise对象上调用.resolve()表示promise已正确返回。同样地,在promise上调用.reject()表示事实已经失败。
If we consider how we actually use a promise:
如果我们考虑如何实际使用承诺:
SomeFactory.getSomething().then(function(data){
//gets called if promise resolves
}, function(error){
//gets called if promise rejected
}
So in the example you have provided, we are checking to see if the response has a 500 error code, and if it does, we are rejecting the promise thus allowing us to handle the error.
因此,在您提供的示例中,我们正在检查响应是否有500错误代码,如果有,我们拒绝承诺,从而允许我们处理错误。
In a responseError
interceptor you have two choices with regards to what you return. If you return $q.reject(response)
you are continuing the error handling chain of events. If you return anything else (generally a new promise or a response) you are indicating that the error has been recovered from and should be treated as a success.
在responseError拦截器中,您有两个关于返回内容的选择。如果返回$ q.reject(响应),则表示您正在继续执行错误处理事件链。如果您返回任何其他内容(通常是新的承诺或响应),则表明错误已从中恢复,应视为成功。
With regards to your two points:
关于你的两点:
1- You need to write that line to indicate that the response should still be considered an error.
1-您需要编写该行以指示响应仍应被视为错误。
2- The effect on the angular app is that the error callback will be called instead of the success callback.
2-对角度应用程序的影响是将调用错误回调而不是成功回调。