我有一连串的承诺,在那里我用捕捉来捕捉错误。
this.load()
.then(self.initialize)
.then(self.close)
.catch(function(error){
//error-handling
})如果链没有被拒绝就完成了,那么调用什么函数?我最后使用了,但是如果发生错误,也会调用它。我想在catch函数之后调用一个函数,这个函数只有在没有承诺被拒绝时才会被调用。
我用的是node.js和Q模块.
发布于 2015-09-21 07:50:25
然后再加一个就行了。
this.load()
.then(self.initialize)
.then(self.close)
.then(function() {
//Will be called if nothing is rejected
//for sending response or so
})
.catch(function(error){
//error-handling
})发布于 2015-09-21 07:50:38
我会将您的.catch()更改为.then(),并同时提供onFullfill和onRejected处理程序。然后,您可以准确地确定发生了哪一种情况,您的代码非常清楚,其中一种或另一种将执行。
this.load()
.then(self.initialize)
.then(self.close)
.then(function() {
// success handling
}, function(error){
//error-handling
});这不是唯一的方法。您还可以使用.then(fn1).catch(fn2),它类似地根据承诺状态之前的内容调用fn1或fn2,但如果fn1返回被拒绝的承诺或抛出异常,则两者都可以被调用,因为这也将由fn2处理。
https://stackoverflow.com/questions/32689844
复制相似问题