Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Status of cancellable promises

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?

like image 963
Pier Paolo Ramon Avatar asked Feb 14 '14 14:02

Pier Paolo Ramon


People also ask

What are the statuses of a Promise?

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.

Is it possible to cancel a Promise?

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 .

How do you stop promises?

You can't cancel a Promise, but you can chain your promises with then , and reject at any point.

How do you cancel a Promise react?

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.


1 Answers

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:

  • Cancellation as a special case of rejections
  • Reactions to cancellation propagate upward in the chain, as well as the rejection itself propagating downward.

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!

like image 104
Domenic Avatar answered Nov 07 '22 12:11

Domenic