bergus / promise-cancellation

Promise cancellation proposal for JavaScript
Other
24 stars 1 forks source link

Cancelling already-resolved promises #6

Open bergus opened 8 years ago

bergus commented 8 years ago

This proposal allows promises to be cancelled through their associated token at any time until it is settled. That means that when they are already resolved but still pending (i.e. resolved with a pending promise), then they might still get cancelled:

const {token, cancel} = CancelToken.source();
setTimeout(cancel, 1000, "over");
const promise = new Promise(resolve => {
    setTimeout(() => {
        resolve(new Promise()); // never resolved, whatever
    }, 500);
}, token);
promise.catch(e => {
    console.log("It's "+e);
});

logs "It's over" after one second.

This is a major difference to Domenics proposal which requires the inner promise to be cancelled for cancellation of the outer, so I would like this discussed.

bergus commented 8 years ago

See now at https://github.com/tc39/proposal-cancelable-promises/issues/53

rtm commented 8 years ago

Hmm, it seems to me that the expression "resolved but stilled pending" is an oxymoron. You are getting caught up in the poor naming of Promise.resolve and the resolve parameter to the executor, which actually mean "resolve or assimilate". I guess it would be better to say "where the promise has assimilated a promise which is still pending".

Terminology aside, it seems to me that you are violating the semantics of assimilation, which is referred to as "adopt its state" in the Promises/A+ spec. Do you not now have the confusing situation where a promise may be canceled EITHER by the assimilated promise canceling OR the original promise canceling?

bergus commented 8 years ago

@rtm "resolved" is the correct and official terminology.

I don't think I'm violating any semantics. If a promise has an associated token, the cancellation races against the settling (fulfillment/rejection) via the resolving functions (including adoption). And no, a promise cannot be cancelled via the assimilated promise, only by its own token and nothing else.