proposal-cancelable-promises | Former home of the now-withdrawn cancelable | Storage library
kandi X-RAY | proposal-cancelable-promises Summary
kandi X-RAY | proposal-cancelable-promises Summary
Former home of the now-withdrawn cancelable promises proposal for JavaScript
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of proposal-cancelable-promises
proposal-cancelable-promises Key Features
proposal-cancelable-promises Examples and Code Snippets
Community Discussions
Trending Discussions on proposal-cancelable-promises
QUESTION
I have a JavaScript (EmberJS + Electron) application that needs to execute sequences of asynchronous tasks. Here is a simplified example:
- Send a message to a remote device
- Receive response less than
t1
seconds later - Send another message
- Receive second response less than
t2
seconds later - Display success message
For simple cases this seems reasonably easy to implement with Promises: 1 then 2 then 3 ... It gets a little trickier when timeouts are incorporated, but Promise.race
and Promise.all
seem like reasonable solutions for that.
However, I need to allow users to be able to cancel a sequence gracefully, and I am struggling to think of sensible ways to do this. The first thing that came to mind was to do some kind of polling during each step to see if a variable someplace has been set to indicate that the sequence should be canceled. Of course, that has some serious problems with it:
- Inefficient: most of the polling time is wasted
- Unresponsive: an extra delay is introduced by having to poll
- Smelly: I think it goes without saying that this would be inelegant. A
cancel
event is completely unrelated to time so shouldn't require using a timer. TheisCanceled
variable may need to be outside of the promise' scope. etc.
Another thought I had was to perhaps race everything so far against another promise that only resolves when the user sends a cancel signal. A major problem here is that the individual tasks running (that the user wants to cancel) don't know that they need to stop, roll-back, etc. so even though the code that gets the promise resolution from the race works fine, the code in the other promises does not get notified.
Once upon a time there was talk about cancel-able promises, but it looks to me like the proposal was withdrawn so won't be incorporated into ECMAScript any time soon though I think the BlueBird promise library supports this idea. The application I'm making already includes the RSVP promise library, so I didn't really want to bring in another one but I guess that's a potential option.
How else can this problem be solved? Should I be using promises at all? Would this be better served by a pub/sub event system or some such thing?
Ideally, I'd like to separate the concern of being canceled from each task (just like how the Promise
object is taking care of the concern of asynchronicity). It'd also be nice if the cancellation signal could be something passed-in/injected.
Despite not being graphically skilled, I've attempted to illustrate what I'm trying to do by making the two drawings below. If you find them confusing then feel free to ignore them.
ANSWER
Answered 2017-Jan-13 at 05:25If I understand your problem correctly, the following may be a solution.
Simple timeoutAssume your mainline code looks like this:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install proposal-cancelable-promises
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page