tc39 / proposal-cancelable-promises

Former home of the now-withdrawn cancelable promises proposal for JavaScript
Other
376 stars 29 forks source link

Rejected CancelTokens #30

Closed bergus closed 8 years ago

bergus commented 8 years ago

The cancel function passed to the executor of a CancelToken is the [[resolve]] function of its .promise. This does let us do weird things like

const rejectedToken = new CancelToken(resolve => resolve(Promise.reject(…)));
const cancelledToken = new CancelToken(resolve => resolve(Promise.cancel(…)));

Are there any use cases for this? They don't make much sense to me. The first one would probably only lead to a huge lot of unhandled rejections. Are token subscribers expected to deal with this?

domenic commented 8 years ago

This is to preserve the invariant that token.promise is not a promise for a promise.

bergus commented 8 years ago

I see. In my approach I just used a token.subscribe(…) method instead of token.promise.then(…) where the callback is called directly with the cancellation reason - no promise resolution involved at all. Of course that means that you'd have to do getEvent().then(cancel) instead of cancel(getEvent()), but I figured the latter is uncommon enough and doesn't provide any advantages.