Q.then
function can actually accept three parameters and all of them should be functions.
The success handler
The failure handler
The progress handler
When you do,
two().then(console.log('good'), console.log('Error is called'));
you are actually passing the result of executing both the console.log
s to the then
function. The console.log
function returns undefined
. So, effectively, you are doing this
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
So, you must be passing two functions to the then
function. Like this
two().then(function() {
// Success handler
console.log('good');
}, function() {
// Failure handler
console.log('Error is called')
});
But, Q
actually provides a convenient way to handle all the errors occur in the promises at a single point. This lets the developer not to worry much about error handling in the business logic part. That can be done with Q.fail
function, like this
two()
.then(function() {
// Success handler
console.log('good');
})
.fail(function() {
// Failure handler
console.log('Error is called')
});