How to cancel/abort a http request?

4,346 views
Skip to first unread message

bin

unread,
Apr 18, 2012, 10:27:10 AM4/18/12
to AngularJS
if an http request has been sent already and it's waiting for the
response, and I want to cancel the request because of some special
user action. How do I do that? I'm using $http (version 1.0.0rc3) to
send the request, which returns a promise, where is the original xhr
object? Please help.

Thanks,
Bin

Igor Minar

unread,
Apr 19, 2012, 10:33:08 AM4/19/12
to ang...@googlegroups.com
You can't at the moment.

But we very much want to have this feature, but it's not trivial because of how promise api works.

We'll make it happen post 1.0.

/i



--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/angular?hl=en.


Mark Waddle

unread,
Sep 25, 2012, 10:51:54 PM9/25/12
to ang...@googlegroups.com
thank you Igor for working on this ... i would like to chime in and say that i could really use this feature as well.

for reference, the issue requesting this feature is here https://github.com/angular/angular.js/issues/1159

On Wednesday, July 18, 2012 12:51:17 PM UTC-7, Eddie Huang wrote:
Any update on this?

I was trying to do promise.reject() on previous $http request when newer request is sent.

Then I realize reject is on the defer object and not o the promise so I'm dead in the water...

Mark Waddle

unread,
Sep 30, 2012, 12:47:38 AM9/30/12
to ang...@googlegroups.com
Hi Igor,

I proposed a possible alternative solution in the issue. I am combing through the angular code right now and attempting to hack together a prototype.

An alternative to enabling request cancellation would be to allow the client to optionally pass in a key for the request and $http would ensure that only one request per key is pending at any point in time. The latest request with a specific key would always trump prior requests with that key. $http would need to cancel any pending requests with the same key before sending the new request. If the key was falsy it would send the request without consideration for pending requests. In other words, any number of requests with falsy keys could be pending simultaneously. The promises for the cancelled requests could be rejected.
This solution would suit my use case very well, and I suspect others' use cases too, and it might also be simpler to implement because you would not need to change the promise interface.



On Thursday, April 19, 2012 7:33:08 AM UTC-7, Igor Minar wrote:

mar...@metapixel.co.uk

unread,
Oct 30, 2012, 1:20:24 PM10/30/12
to ang...@googlegroups.com
Hi Mark,

Did you have any luck with your prototype?

Mark Waddle

unread,
Nov 1, 2012, 3:14:06 AM11/1/12
to ang...@googlegroups.com, mar...@metapixel.co.uk
No, not really. I was able to surface a monkey-patched function on the promise that allowed the request to be cancelled and the error callback to be called with a status of -2 so it could be distinguished from a normal network error, however it was a terrible hack. Igor is absolutely correct when he states it is not trivial due to the promise API. A robust and tested solution would take a significant amount of time. I eventually gave up to focus on higher priorities.
Reply all
Reply to author
Forward
0 new messages