我正在尝试找出一个好方法来表达“做所有这些事情,但在其中任何一个失败的情况下保释”
我现在拥有的:
var defer = $q.defer();
this
.load( thingy ) // returns a promise
.then( this.doSomethingA.bind( this ) )
.then( this.doSomethingB.bind( this ) )
.then( this.doSomethingC.bind( this ) )
.then( this.doSomethingD.bind( this ) )
.then( function(){
defer.resolve( this );
} );
;
return defer.promise;
我最终想要的是以某种方式捕获该链上的任何错误,以便我可以将其传递给上面的 defer
promise。我并不特别关心语法是否与我上面的相似。
或者即使任何人都可以告诉我如何停止上述链。
最佳答案
您可以通过在任何 then 回调中返回被拒绝的 promise 来停止 angularjs 链。
load()
.then(doA)
.then(doB)
.then(doC)
.then(doD);
doA、doB、doC、doD 可以有这样的逻辑:
var doA = function() {
if(shouldFail) {
return $q.reject();
}
}
关于javascript - 停止 AngularJS promise 链,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16847413/