Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

feat($q): Add optional canceler and promise.cancel method #2452

Closed
wants to merge 1 commit into from

Conversation

dbinit
Copy link
Contributor

@dbinit dbinit commented Apr 19, 2013

Add an optional argument to $q.defer to provide a canceling function.
This exposes a promise.cancel method that can be used to call the
canceling function and reject the promise.

See existing implementations in Dojo Toolkit and Promised-IO library
for reference.

This will make it easier to implement things like #1159.

@caiotoon
Copy link
Contributor

Excellent! I believe this is a very important feature. With a optional canceler from a promise, we would be able to let all the logic regarding the canceling in the same place as the starting.

+1

@IgorMinar
Copy link
Contributor

How is this going to work with promise chains, joins and forks? This is the hard part about this feature.

Let's say that you have this scenario:

var promiseA = $http.get('/foo');
promiseB1 = promiseA.then(doSomething);
promiseB2 = promiseA.then(doSomethingElse);

promiseB2.cancel();

what happens to promiseA and promiseB1?

@IgorMinar
Copy link
Contributor

one of the guiding principles of promise paradigm is that the communication is one way from the resolver to the promise consumer. your cancel makes the communication bidirectional which introduces these problems that I illustrated above.

@dbinit
Copy link
Contributor Author

dbinit commented Apr 25, 2013

Thanks for taking a look Igor.

I've added more tests and, as a result, cleaned up the implementation a bit.

In the scenario you mentioned (I've added a test), all derived promises would be canceled. In the case of something like $q.all(), it does not implement cancellation, so it would not be available on the resulting promise.

Dojo has a decent description.

Add an optional argument to $q.defer to provide a canceling function.
This exposes a promise.cancel method that can be used to call the
canceling function and reject the promise (and all derived promises).
See existing implementations in Dojo Toolkit and Promised-IO library
for reference.
@dbinit
Copy link
Contributor Author

dbinit commented Apr 30, 2013

Closing this as it's unlikely to go through... and I think I prefer #2529 approach.

@akarelas
Copy link

pitty this was cancelled

@dbinit
Copy link
Contributor Author

dbinit commented Sep 24, 2013

@akarelas, #2529 was merged instead.

@blabno
Copy link

blabno commented Sep 4, 2014

Is http the only "promise" you are using?
What if my promise resolves on $window.setTimeout and it has nothing to do with $http? Or what if $http is wrapped with several layers of abstraction?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants