Say I have the following Promise chain:
var parentPromise = Promise.resolve()
.then(function () {
var condition = false;
if (condition) {
return parentPromise.cancel('valid reason');
} else {
return Promise.resolve()
.then(function () {
var someOtherCondition = true;
if (someOtherCondition) {
console.log('inner cancellation');
return parentPromise.cancel('invalid reason');
}
});
}
})
.catch(Promise.CancellationError, function (err) {
console.log('throwing');
if (err.message !== 'valid reason') {
throw err;
}
})
.cancellable();
The above never enters the catch
.
If we swap condition
to true
, the inner cancellation is never hit, but the catch
is still not triggered.
removing the .cancellable
at the end , and replacing all instances of parentPromise.cancel()
with explicit throw new Promise.CancellationError()
"fixes" the problem. What I don't understand is why?
Why was the original approach not working?
I am using bluebird 2.3.11.
cancellable()
creates cancellable promises and only they throwCancellationError
by default, whencancel
function is called with out any reason.In your case, you are making the promise
cancellable
only after attaching thecatch
handler. But the promise is notcancellable
yet. So,cancel
function call will not raisePromise.CancellationError
.You need to change the structure of the code, like this
Note: I would recommend promising with its beautiful
Promise
constructor function. It is similar to the ECMA Script 6 specification.