-
Notifications
You must be signed in to change notification settings - Fork 27.5k
Feature: Cancel $http request #1159
Comments
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(); |
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. |
Me too, need that feature. |
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. |
+1 for this feature. |
+1 from me as well |
+1 |
2 similar comments
+1 |
+1 |
+1 |
1 similar comment
+1 |
@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. |
Need this feature! +1 |
+1 |
3 similar comments
+1 |
+1 |
+1 |
dbinit's solution worked perfectly for me. |
+1, @dbinit this new fix looks really nice! |
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(); 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 that sounds like a separate request deserving of its own issue
On Wed, Jan 30, 2013 at 4:07 PM, Ryan Shaw notifications@github.com wrote:
|
@markwaddle, I just filed it as a separate issue in #1934 |
+1 |
2 similar comments
+1 |
+1 |
Any reason this shouldn't work? Because it doesn't fire the request at all.. el.bind('keyup', function() {
var canceler = $q.defer();
$http.post('/api', data, {timeout: canceler.promise}).success(function(returnData) {
});
canceler.resolve();
}); |
You need to wrap your code with scope.$apply. Bind doesn't do it implicitly. |
$q needs it? Becuase $http doesn't.. anyway it' still not firing the request no matter if I put it inside |
Hmm, I think the $http.post doesn't fire the request until the next digest cycle. So the canceler.resolve is canceling it before it happens. Try putting a scope.$apply between the $http.post and canceler.resolve. |
It seems to me like it is working. |
Ehm, that's AngularJS. Every external event call must invoke the scope digester. $http will create the request with the next scope cycle.. the example above have not one. |
@dbinit that works, thanks! |
See #2442 for the issue reporting on $http requiring to be called within a $watch. That really sounds like a bug to me, as one should be able to call $http from any context. |
great thing. thank you very much! |
Seems like I'm about 4 months too late but doesn't the approach of hijacking the timeout parameter means that you can't have both a fixed timeout AND the option to manually cancel (due to the request being made obsolete) without rolling your own timeout functionality? That seems rather limited. Do we know if separating this joint functionality into discrete parts is on the roadmap at all? |
@markstickley It's just a promise you control, it has nothing to do with timeout |
@markstickley You can just use the promise returned by $timeout and then do $timeout.cancel(promise) if you want to cancel the request early. |
@dbinit That's a decent workaround, thanks I'll try that :) |
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. |
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. I modified @btford 's plunk to demonstrate this: http://plnkr.co/edit/BW6Zwu?p=preview |
has this stopped working for anyone in 1.2.1? |
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?
|
@anton000 your code only rejects the wrapper promise. it doesn't actually cancel original $http promise |
@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 . |
I cannot get it to cancel my request. used this approach: and this: What is going on, I really need this. |
@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 |
+1 |
How is going this feature? I need it a lot!!! 👍 |
@codeniac read here https://docs.angularjs.org/api/ng/service/$http#usage about |
couldn't you use |
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
The text was updated successfully, but these errors were encountered: