请原谅我对promise概念的陌生。我在Node.js中使用Q模块。我有一个函数,它的目的是在执行所有必要的步骤后调用回调函数。问题出现在当我想从Q promise内部调用回调函数时。
我的期望功能是在到达最后一步时调用回调函数,并不再在promise链中。因此,回调函数将恢复其原始操作。然而,按照我的编码方式,回调函数在promise的上下文中被调用。此时,如果回调(比如)抛出一个错误,它会被这个函数中的错误处理程序捕获,这不是我想要的!
在这种情况下,回调函数中发生的错误会导致打印出"OHNOES!!!!!"。
我的期望功能是在到达最后一步时调用回调函数,并不再在promise链中。因此,回调函数将恢复其原始操作。然而,按照我的编码方式,回调函数在promise的上下文中被调用。此时,如果回调(比如)抛出一个错误,它会被这个函数中的错误处理程序捕获,这不是我想要的!
var updateDataStream = function(data, input, posts, stream, callback) {
// Pack all the items up...
Q.ncall(data._packStream, data, posts, stream)
// Upsert the cache into the database
.then(function(){
return Q.ncall(data.upsert, data);
})
// buffer the new input
.then(function(res){
return Q.ncall(data.buffer, data, input);
})
.then(function(final){
callback(null, final);
})
.fail(function(err){
console.log('OHNOES!!!!!!!',err);
}).end();
}
在这种情况下,回调函数中发生的错误会导致打印出"OHNOES!!!!!"。