Mar*_*ahn 8 javascript angularjs
我正在试图找出一个好方法来说"做所有这些事情,但在任何一个失败的情况下保释"
我现在拥有的:
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;
Run Code Online (Sandbox Code Playgroud)
我最终想要的是以某种方式捕获该链上的任何错误,以便我可以将其传递给defer
上面的承诺.如果语法与上面的语法类似,我并不特别在意.
或者即使有人可以告诉我如何阻止上述链条.
你可以通过在任何回调中返回被拒绝的promise来停止angularjs链.
load()
.then(doA)
.then(doB)
.then(doC)
.then(doD);
Run Code Online (Sandbox Code Playgroud)
其中DOA,DOB,DOC,DOD可具有逻辑是这样的:
var doA = function() {
if(shouldFail) {
return $q.reject();
}
}
Run Code Online (Sandbox Code Playgroud)
您应该能够通过以下方式做同样的事情:
var defer = $q.defer();
this
.load( thingy ) // returns a promise
.then( this.doSomethingA.bind( this ), $q.reject )
.then( this.doSomethingB.bind( this ), $q.reject )
.then( this.doSomethingC.bind( this ), $q.reject )
.then( this.doSomethingD.bind( this ), $q.reject )
.then( defer.resolve.bind( defer, this ), defer.reject.bind( defer ) );
;
return defer.promise;
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
8787 次 |
最近记录: |