抓后,然后执行 [英] Executing then after catch
问题描述
我有以下的小提琴:
http://jsfiddle.net/thelgevold/3uv9nnjm/6/
angular.module('hello',[]).controller('helloController',function($q){
console.clear();
function someService(){
var deferred = $q.defer();
deferred.reject({e:'error'});
return deferred.promise;
}
function callService(){
return someService().then(function(obj){
console.log('first then');
}).
catch(function(e){
console.log('error1');
var deferred = $q.defer();
deferred.reject({e:'error'});
return deferred.promise;
});
}
callService().catch(function(e){
console.log('error2');
}).then(function(e){
console.log('second then');
});
});
它本质上只是一个快速$ Q承诺POC。我的问题是:为什么最后再子句时得到的承诺被拒绝叫什么名字?输出如下所示:
It's essentially just a quick $q promise POC. My question is: Why does the last then clause get called when the promise is rejected? The output is as follows:
ERROR1
误差2
第二次再
我明白为什么ERROR1 /误差2打印,但我认为第二个字符串,然后不应该因为打印的承诺遭到了拒绝。我认为这将省去第二次则,为第一则省略同样的道理。
有什么想法?
I understand why error1/error2 are printed, but I thought the second then string should not be printed since the promise was rejected. I thought it would omit "second then" for the same reason the "first then" is omitted. Any thoughts?
推荐答案
在我开始之前,不这样做:
Before I get started, don't do this:
var deferred = $q.defer();
deferred.reject({e:'error'});
return deferred.promise;
做到这一点:
return $q.reject({e:'error'});
或者preferably,这样的:
Or preferably, this:
return $q.reject(new Error('error'));
<一个href=\"http://stackoverflow.com/questions/23803743/what-is-the-deferred-antipattern-and-how-do-i-avoid-it\">Beware递延反模式。
现在,在回答你的问题。
在
.catch()
您的来电 callService()
正在迎头赶上错误,而不是产生一个新的错误后。它基本上是处理的错误,下面。然后()
的处理程序是免费的调用。
Now, for the answer to your question.
The
.catch()
after your call to callService()
is catching the error and not producing a new error. It has essentially "handled" the error, and the following .then()
handler is free to be called.
同步code相当于你的例子是:
The synchronous code equivalent of your example would be:
function someService() {
throw {e: 'error'};
}
function callService(){
try {
var obj = someService();
console.log('first then');
} catch (e) {
console.log('error1');
throw {e: 'error'};
}
}
var e;
try {
e = callService();
} catch (e) {
console.log('error2');
}
console.log('second then');
我认为,如果你看它这样,它非常有意义。
I think that if you look at it this way, it makes perfect sense.
在承诺/ A +规格中的有关内容这里。对于所有意图和目的,可以查看抓
处理程序,同样的事情作为一个 onRejected
处理程序:
The relevant text in the Promises/A+ spec is here. For all intents and purposes, you can view the catch
handler as the same thing as an onRejected
handler:
2.2.7。那么必须返回一个承诺[3.3]。
2.2.7. then must return a promise [3.3].
promise2 = promise1.then(onFulfilled,onRejected);
2.2.7.1。如果任onFulfilled或onRejected返回一个值x,运行无极解决程序[解析](promise2,X)。
2.2.7.1. If either onFulfilled or onRejected returns a value x, run the Promise Resolution Procedure [[Resolve]](promise2, x).
基本上,你的 onRejected
处理程序返回值未定义
,因此所产生的诺言赶上()
与价值解析未定义
。
Basically, your onRejected
handler is "returning" the value undefined
, so the promise produced by catch()
resolves with the value undefined
.
这篇关于抓后,然后执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!