Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rejection turns into fulfillment #23

Closed
brandf opened this issue Oct 22, 2013 · 3 comments
Closed

rejection turns into fulfillment #23

brandf opened this issue Oct 22, 2013 · 3 comments

Comments

@brandf
Copy link

brandf commented Oct 22, 2013

This has me puzzled, and I can't find justification for it in the Promise/A+ spec.

var r = Promise.pending();
r.promise.catch(Promise.CancellationError,
               function () { console.log("cancel"); })
         .then(function () { console.log("fulfilled"); });
r.cancel();

When I do the above I was expecting that I'd get "cancel" in the output, but I got "cancel" and "fulfilled".

It turns out this is because my rejection handler didn't return anything, so the return value of undefined was promoted into a fulfilled promise! I can't find anything in Promise/A+ that indicates this should happen. I would argue that if you don't return anything, the .then promise should take the state of the promise that triggered the .catch.

This would prevent a rejection from unexpectedly being converted to a fulfillment.

To work around the issue, I explicitly return Promise.rejected(reason) form the .catch callback, but this requires allocating a new promise. What I really want to say is 'just use the existing promise', but I don't see any way to do that.

@domenic
Copy link

domenic commented Oct 22, 2013

If you don't re-throw the error in the catch handler, it is … handled. This is just like how sync try/catch works. It's not an unexpected conversion; it's a conversion.

Promises/A+ covers this fully; it makes no exception for undefined.

@petkaantonov
Copy link
Owner

Your code parallels:

try {
    throw new CancellationError();
}
//Imagine this syntax working
catch(CancellationError e) {
    console.log("cancel");
}
console.log("fulfilled");

It looks like you want:

try {
    throw new CancellationError();
}
//Imagine this syntax working
catch(CancellationError e) {
    console.log("cancel");
    throw e;
}
console.log("fulfilled");

Which can be done:

var r = Promise.pending();
r.promise.catch(Promise.CancellationError, function (e) {
    console.log("cancel");
    throw e;
})
.then(function () {
    console.log("fulfilled");
});
r.cancel();

And gives:

cancel
Possibly unhandled CancellationError: cancellation error
    at Promise$cancel [as cancel] (<anonymous>:1200:19)
From previous event:
    at <anonymous>:2:17
    at Object.InjectedScript._evaluateOn (<anonymous>:580:39)
    at Object.InjectedScript._evaluateAndWrap (<anonymous>:539:52)
    at Object.InjectedScript.evaluate (<anonymous>:458:21)

@brandf
Copy link
Author

brandf commented Oct 30, 2013

Yes, I agree this makes sense, and I was simply confused.

@brandf brandf closed this as completed Oct 30, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants