The oldest issue on https://github.com/promises-aplus/cancellation-spec is (at the time of writing) 9 months old. I really can’t found a reliable source of information about cancellation features on ‘standard’ promises.
By now looks like the feature is implemented in bluebird, but as a library developer I don’t want to clutter my package with a full promise implementation.
What I’d like to do is simply pass a promise-like and support the cancellation-spec.
Where could I find this information?
A Promise is in one of these states: pending: initial state, neither fulfilled nor rejected. fulfilled: meaning that the operation was completed successfully. rejected: meaning that the operation failed.
In modern JavaScript - no Promises have settled (hah) and it appears like it will never be possible to cancel a (pending) promise. Instead, there is a cross-platform (Node, Browsers etc) cancellation primitive as part of WHATWG (a standards body that also builds HTML) called AbortController .
You can't cancel a Promise, but you can chain your promises with then , and reject at any point.
Promises once fired, cannot be cancelled. So cancelling the promises in our current context means to ignore the result of the promise. When an api call is fired inside a react component, any state update after the component is destroyed (inside the then block of the promise), will cause error.
Cancellable promises are not going to be in ES6, as ES6 promises are very minimal.
Work on cancellation in the Promises/A+ space has stalled, as we wait for library evolution to prove one approach clearly superior. The latest thinking is at this issue, which is what most libraries looking to implement cancellation seem to follow (more or less). The key points are:
It's not clear what you mean by "simply pass a promise-like and support the cancellation-spec." Are you trying to produce thenables, under the assumption that consumers of your library will cast it, but somehow inherit some cancellation behavior afterward? That'd be a bit tricky, especially since cancellation generally depends on a specified Cancellation
constructor used for rejecting the promise. If the cancellation ecosystem was more developed, the way to do this would likely be more straightforward.
As for the future, well, it's in flux! One route forward would be for someone to champion an evolution of that cancellation proposal in Promises/A+ space, getting implementer buy-in from major libraries like Q, RSVP, when, and Bluebird. Then a lot of the smaller libraries would likely buy in, and you'd have something you could probably depend on. If it proves that popular, it'd probably be considered for ECMAScript promises as well!
But that depends on a lot of people doing a lot of work, so we'll see if it happens :). It was kind of a miracle for it to happen with the base Promises/A+ spec, but who knows... it could happen again!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With