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

Feature: Cancel $http request #1159

Closed
Closed

Description

@coli

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

Activity

  1. coli commented on Jul 18, 2012

    @coli
    Author

    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();

  2. markwaddle commented on Sep 26, 2012

    @markwaddle

    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.

  3. koistya commented on Sep 30, 2012

    @koistya

    Me too, need that feature.

  4. markwaddle commented on Sep 30, 2012

    @markwaddle

    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.

  5. samunplugged commented on Oct 20, 2012

    @samunplugged

    +1 for this feature.

  6. jbeard4 commented on Oct 31, 2012

    @jbeard4

    +1 from me as well

  7. gregwebs commented on Nov 13, 2012

    @gregwebs

    +1

  8. jinder commented on Nov 14, 2012

    @jinder

    +1

  9. marknadig commented on Nov 20, 2012

    @marknadig
    Contributor

    +1

  10. sssilver commented on Nov 30, 2012

    @sssilver

    +1

  11. rickkln commented on Dec 2, 2012

    @rickkln

    +1

  12. gonzaloruizdevilla commented on Dec 12, 2012

    @gonzaloruizdevilla
    Contributor

    @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.

  13. nicolacity commented on Dec 27, 2012

    @nicolacity

    Need this feature! +1

  14. amirnissim commented on Jan 2, 2013

    @amirnissim

    +1

  15. 57 remaining items

  16. markstickley commented on Oct 3, 2013

    @markstickley

    @dbinit That's a decent workaround, thanks I'll try that :)

  17. btford commented on Oct 4, 2013

    @btford
    Contributor

    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.

  18. derekrprice commented on Nov 7, 2013

    @derekrprice

    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.

  19. phantomwhale commented on Nov 13, 2013

    @phantomwhale

    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

  20. fxck commented on Nov 19, 2013

    @fxck

    has this stopped working for anyone in 1.2.1?

  21. anton000 commented on Feb 19, 2014

    @anton000

    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?

    $scope.fetchData = function() {
      if ($scope.async) {
        $scope.async.reject();
      }
      $scope.async = testAsync();
      $scope.async.promise.then(
        function(data){
        console.log('success' + data);
        },
        function(data) {
          console.log('reject' + data);
        }
      );
    }
    
    testAsync = function() {
      var deferred = $q.defer();
      $http.get('/api/')
        .success(function(data) {
          deferred.resolve(data);
        })
        .error(function(data) {
          deferred.reject(data);
        });
      return deferred;
    };
    
  22. snjoetw commented on Mar 28, 2014

    @snjoetw

    @anton000 your code only rejects the wrapper promise. it doesn't actually cancel original $http promise

  23. anton000 commented on Mar 29, 2014

    @anton000

    @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 .

  24. anton000 commented on Jul 8, 2014

    @anton000

    @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

  25. BiosSun commented on Dec 4, 2014

    @BiosSun

    +1

  26. codeniac commented on Apr 1, 2015

    @codeniac

    How is going this feature? I need it a lot!!! 👍

  27. e-oz commented on Apr 1, 2015

    @e-oz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions