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

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

Closed
dbinit wants to merge 1 commit into
angular:masterfrom
dbinit:q_cancel
Closed

feat($q): Add optional canceler and promise.cancel method#2452
dbinit wants to merge 1 commit into
angular:masterfrom
dbinit:q_cancel

Conversation

@dbinit

Copy link
Copy Markdown
Contributor

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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
ContributorAuthor

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
Copy Markdown
ContributorAuthor

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

@dbinitdbinit closed this Apr 30, 2013
@dbinitdbinit mentioned this pull request Jun 6, 2013
@akarelas

Copy link
Copy Markdown

pitty this was cancelled

@dbinit

Copy link
Copy Markdown
ContributorAuthor

@akarelas, #2529 was merged instead.

@blabno

Copy link
Copy Markdown

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 freeto 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

@dbinit@caiotoon@IgorMinar@akarelas@blabno