angular.js icon indicating copy to clipboard operation
angular.js copied to clipboard

Feature: Cancel $http request

Open coli opened this issue 14 years ago • 73 comments

Use case is we started a new $http request, and therefore needs to cancel 1 or more previous $http requests.

Eg:

var previousPromises = []; previousPromises.push($http().then(function(){})); ...

Then: //previousPromises[0].cancel() or //previousPromises[0].reject()

Reference https://groups.google.com/forum/?fromgroups#!searchin/angular/cancel$20http/angular/aT-MowZoPJg/kPEr5Ja0GEwJ

coli avatar Jul 18 '12 19:07 coli

A slightly related feature is ability to cancel a promise, or if the promise has a reference to the defer, then we can do promise.defered.reject();

coli avatar Jul 18 '12 20:07 coli

i need this feature as well.

i have an interface where a user can quickly trigger many actions that in turn trigger $http requests. in the case that a request is triggered and one is currently waiting for a response, i would like to be able to cancel it before requesting the new one.

markwaddle avatar Sep 26 '12 02:09 markwaddle

Me too, need that feature.

koistya avatar Sep 30 '12 00:09 koistya

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.

markwaddle avatar Sep 30 '12 04:09 markwaddle

+1 for this feature.

samunplugged avatar Oct 20 '12 20:10 samunplugged

+1 from me as well

jbeard4 avatar Oct 31 '12 20:10 jbeard4

+1

gregwebs avatar Nov 13 '12 14:11 gregwebs

+1

jinder avatar Nov 14 '12 16:11 jinder

+1

marknadig avatar Nov 20 '12 17:11 marknadig

+1

sssilver avatar Nov 30 '12 18:11 sssilver

+1

rickkln avatar Dec 02 '12 22:12 rickkln

@dbinit I think you should reject the promise when the abort method is called. The function that will handle the rejection can use the status parameter to find out if it was an abort request or different kind of error and then act consequently.

gonzaloruizdevilla avatar Dec 12 '12 14:12 gonzaloruizdevilla

Need this feature! +1

nicolacity avatar Dec 27 '12 22:12 nicolacity

+1

amirnissim avatar Jan 02 '13 12:01 amirnissim

+1

johnoscott avatar Jan 10 '13 21:01 johnoscott

+1

georgiosd avatar Jan 14 '13 19:01 georgiosd

+1

fredrikbonander avatar Jan 16 '13 15:01 fredrikbonander

dbinit's solution worked perfectly for me.

rickkln avatar Jan 16 '13 15:01 rickkln

+1, @dbinit this new fix looks really nice!

ryankshaw avatar Jan 26 '13 01:01 ryankshaw

another thing that i need is to be able to bind to the progress event of the xhr upload.

for example (in plain javascript):

var xhr = new XMLHttpRequest(); xhr.upload.addEventListener("progress", uploadProgress, false)

I want to be able to do the same thing by doing $http.post...

so maybe rather than just adding an abort method, it would be good to provide something that lets me just get at the original xhr object.

ryankshaw avatar Jan 31 '13 00:01 ryankshaw