Q.then function can actually accept three parameters 和都应该是函数。
成功处理程序
故障处理程序
进度处理程序
当你这样做时,
two().then(console.log('good'), console.log('Error is called'));
您实际上是将执行console.logs 的结果传递给then 函数。 console.log 函数返回 undefined。所以,实际上,你正在这样做
var first = console.log('good'); // good
var second = console.log('Error is called'); // Error is called
console.log(first, second); // undefined, undefined
two().then(first, second); // Passing undefineds
因此,您必须将两个函数传递给then 函数。像这样
two().then(function() {
// Success handler
console.log('good');
}, function() {
// Failure handler
console.log('Error is called')
});
但是,Q 实际上提供了一种方便的方法来处理在单点发生的所有错误。这让开发人员不必担心业务逻辑部分中的错误处理。这可以通过Q.fail 函数来完成,就像这样
two()
.then(function() {
// Success handler
console.log('good');
})
.fail(function() {
// Failure handler
console.log('Error is called')
});