bergus / promise-cancellation

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

Assimilation of legacy cancellable promises #8

Open bergus opened 8 years ago

bergus commented 8 years ago

Most of the existing implementations do offer a .cancel() method on promises. It would be super slick if we could invoke this when assimilating such a cancellable thenable together with a token (apart from already passing the token into then). I'd imagine doing something like

…
token.subscribe(reason => {
   const cancel = thenable.cancel;
   if (typeof cancel == "function")
       cancel.call(thenable, reason);
});
then.call(thenable, resolve, reject, token);

in the assimilation sequence (PromiseResolveThenableJob).

This would allow us to do something like

const Bluebird = require("bluebird");
Bluebird.configure({ cancellation: true });
const bp = … // some cancellable Bluebird promise

const {token, cancel} = CancelToken.source();
const promise = Promise.resolve(bp, token);

// or

const p = … // some ES8 promise
p.then(val => {
     const bp = … // some cancellable Bluebird promise
     return bp;
}, token)

where cancel() later would invoke bp.cancel().

Is this feasible? What do you think? Pinging @petkaantonov @benjamingr

bergus commented 8 years ago

I guess the more reasonable solution would be update legacy libraries to deal with the token they are passed, just like tasks should do. No need to hassle with this in the spec.