indapublic
indapublic

Reputation: 2328

Q.Promise: How to throw exception in callback correctly?

My simple code here http://jsfiddle.net/xh6960fo/

function test () {
    var res = Q.defer();
    res.resolve('Hello');
    return res.promise;
};

test()
.then(
    function(message) {
        console.log(message);
        throw new Error('Exception!');
    },
    function (err) {
        console.log('no');
        console.error(err);
    })
.fin(function () {
    console.log('fin');
});

I need to raise exception in 'then' callback.

But in console I see only

Hello

fin

My exception not raising. How to throw exception correctly?

Upvotes: 1

Views: 103

Answers (1)

sdgluck
sdgluck

Reputation: 27257

A then's exception handler only catches errors that occur before its own success handler, not including.

...
.then(function() {
    throw new Error('error'):
})
.then(null, function(err) {
    console.log(err);
})
.fin(...)

Upvotes: 1

Related Questions