[英]Angular's $q.reject() vs deferred.reject()
I'm trying to get a handle on the Angular $q
service and its related objects and APIs. 我正在尝试处理Angular
$q
服务及其相关对象和API。 When I look at the objects in my console I see: 当我在控制台中查看对象时,我看到:
var deferred = $q.defer()
...(and then from console inspection)...
$q: Object {defer: function, reject: function, when: function, all: function}
deferred: Object {resolve: function, reject: function, notify: function, promise: Object}
deferred.promise: Object {then: function, catch: function, finally: function}
It raises a few questions: 它提出了几个问题:
$q.reject()
and deferred.reject()
? $q.reject()
和deferred.reject()
之间有什么区别? When to use each? errorFn
in deferred.promise.then(successFn, errorFn)
and the catchFn
in deferred.promise.catch(catchFn)
? errorFn
在deferred.promise.then(successFn, errorFn)
和catchFn
在deferred.promise.catch(catchFn)
catch()
function always be called? catch()
函数吗? What if one of the nested promises also has a catch function defined? Thanks. 谢谢。
1) $q.reject()
is a shortcut to create a deferred and then reject it immediately; 1)
$q.reject()
是创建延迟的快捷方式,然后立即拒绝它; I often use this in an errorFn if I can't handle the error. 如果我无法处理错误,我经常在errorFn中使用它。
2) Nothing, promise.catch(errorFn)
is just syntactic sugar for promise.then(null, errorFn)
, just like the success and error methods of the $http
service, so you can write code like the following: 2)没有,
promise.catch(errorFn)
只是promise.catch(errorFn)
promise.then(null, errorFn)
语法糖,就像$http
服务的成功和错误方法一样,所以你可以编写如下代码:
promise.
then(function(result){
// handle success
return result;
}, function errorHandler1(error){
// handle error, exactly as if this was a separate catch in the chain.
}).catch(function errorHandler2(error){
// handle errors from errorHandler1
});
3) This is exactly where $q.reject can come in handy: 3)这正是$ q.reject可以派上用场的地方:
promise.
catch(function(error){
//Decide you can't handle the error
return $q.reject(error); //This forwards the error to the next error handler;
}).catch(function(error){
// Here you may handle the error or reject it again.
return 'An error occurred';
//Now other errorFn in the promise chain won't be called,
// but the successFn calls will.
}).catch(function(error){
// This will never be called because the previous catch handles all errors.
}).then(function(result){
//This will always be called with either the result of promise if it was successful, or
//'An error occured' if it wasn't
});
Ok this is my take from promises. 好的,这是我对承诺的看法。
$q.reject(reason)
returns a rejected promise with the reason passed as argument and defered. $q.reject(reason)
返回一个被拒绝的promise,其原因是作为参数和deferred传递的。 Reject rejects an existent defered whether its process has finished or not. 拒绝拒绝现有的否定其过程是否已完成。
errorFn
gets launched when a promise is rejected and its argument is the reason why it was rejected. 当promise被拒绝并且其参数是被拒绝的原因时,将启动
errorFn
。 Catch is called when an error inside the promise process wasn't handled properly causeing the promise to raise and exception and not being either rejected or fulfilled . 如果未正确处理承诺流程中的错误导致承诺提升和异常而不被拒绝或履行,则调用Catch。
You shoudn't have nested promises, you should have chained promises in which case the latest catch block should catch everything prior to it if no other block was specified to handle it. 你没有嵌套的promises,你应该有链接的promises,在这种情况下,如果没有指定其他块来处理它,最新的catch块应该先捕获它之前的所有内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.