You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
Repository navigation
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
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.
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.
@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.
btford's plunker isn't completely working for me. The cancelQuery = null in $http.post().success() backfires. success() gets called asynchronously for cancelled requests, so with a query launched on keypress, if I type quickly some of the cancelQuery info is lost, meaning only some requests are cancelled. If I just remove the cancelQuery = null line, everything works fine.
I ended up bringing in a boolean flag, so that I could differentiate between a "normal" error, and a manually cancelled error. The need to do this does seem to highlight this is indeed a "workaround" and perhaps could do with some better direct support at a later stage.
instead of relying on timeout, ended up wrapping $http in another deferred, then rejected that deferred when a new call was made or when i needed to "cancel". this had the result of still completing the $http call, but rejecting the data returned.
@snjoetw i explained in my post it still completes the original $http promise but rejects the data returned. I think using this matches the concept of promise to resolver communication being unidirectional as @IgorMinar explained in #2452 .
Activity
coli commentedon Jul 18, 2012
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();
markwaddle commentedon Sep 26, 2012
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.
koistya commentedon Sep 30, 2012
Me too, need that feature.
markwaddle commentedon Sep 30, 2012
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.
samunplugged commentedon Oct 20, 2012
+1 for this feature.
jbeard4 commentedon Oct 31, 2012
+1 from me as well
gregwebs commentedon Nov 13, 2012
+1
jinder commentedon Nov 14, 2012
+1
marknadig commentedon Nov 20, 2012
+1
Added new feature to abort a pending request. Fixes issue angular#1159.
sssilver commentedon Nov 30, 2012
+1
rickkln commentedon Dec 2, 2012
+1
gonzaloruizdevilla commentedon Dec 12, 2012
@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.
nicolacity commentedon Dec 27, 2012
Need this feature! +1
amirnissim commentedon Jan 2, 2013
+1
57 remaining items
markstickley commentedon Oct 3, 2013
@dbinit That's a decent workaround, thanks I'll try that :)
btford commentedon Oct 4, 2013
I wrote a service demonstrating how one might encapsulate this functionality here: http://plnkr.co/edit/P8wKns51GVqw5mwS5l5R?p=preview
I thought it might be useful for anyone that comes across this thread in the future.
derekrprice commentedon Nov 7, 2013
btford's plunker isn't completely working for me. The cancelQuery = null in $http.post().success() backfires. success() gets called asynchronously for cancelled requests, so with a query launched on keypress, if I type quickly some of the cancelQuery info is lost, meaning only some requests are cancelled. If I just remove the cancelQuery = null line, everything works fine.
phantomwhale commentedon Nov 13, 2013
I ended up bringing in a boolean flag, so that I could differentiate between a "normal" error, and a manually cancelled error. The need to do this does seem to highlight this is indeed a "workaround" and perhaps could do with some better direct support at a later stage.
I modified @btford 's plunk to demonstrate this: http://plnkr.co/edit/BW6Zwu?p=preview
angular : $http timeout can be number or promise
fxck commentedon Nov 19, 2013
has this stopped working for anyone in 1.2.1?
anton000 commentedon Feb 19, 2014
instead of relying on timeout, ended up wrapping $http in another deferred, then rejected that deferred when a new call was made or when i needed to "cancel". this had the result of still completing the $http call, but rejecting the data returned.
thoughts?
snjoetw commentedon Mar 28, 2014
@anton000 your code only rejects the wrapper promise. it doesn't actually cancel original $http promise
anton000 commentedon Mar 29, 2014
@snjoetw i explained in my post it still completes the original $http promise but rejects the data returned. I think using this matches the concept of promise to resolver communication being unidirectional as @IgorMinar explained in #2452 .
m0xj commentedon Jul 8, 2014
I cannot get it to cancel my request.
used this approach:
http://www.bennadel.com/blog/2624-aborting-an-ajax-request-in-angularjs-using-httpi.htm?&_=0.5996839550789446#comments_45274
and this:
http://developer.rackspace.com/blog/cancelling-ajax-requests-in-angularjs-applications.html
What is going on, I really need this.
anton000 commentedon Jul 8, 2014
@mato75 This would be better asked on the blog itself which you got the snippet from, or over on sites like http://www.stackoverflow.com
BiosSun commentedon Dec 4, 2014
+1
codeniac commentedon Apr 1, 2015
How is going this feature? I need it a lot!!! 👍
e-oz commentedon Apr 1, 2015
@codeniac read here https://docs.angularjs.org/api/ng/service/$http#usage about
timeoutargument.