Angular – abort ajax request when using Restangular

All we need is an easy explanation of the problem, so here it is.

I have a method that calls an angular service and consequently makes an ajax request via the service. I need to make sure that if this is called several times, the previous request in aborted (if it hasn’t already been resolved that is).

This method can get called multiple times. This method is actually from ngTable on ngTableParams:

getData = function($defer, params) {

      myService.getRecord(params).then(function(res){ 
             ...
             $defer.resolve(res.Records);
      }); 
}

Here’s the method on the service:

this.getRecords = function(params) {
    ...

    return Restangular
          .all('/api/records')
          .post(filters);
};

If ngTable makes 3 calls I want the first 2 to be aborted (unless of course they returned so fast that it got resolved)

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

You can abort $http calls via the timeout config property, which can be a Promise, that aborts the request when resolved.

So in restangular, you can do this like

var abort = $q.defer();
Restangular.one('foos', 12345).withHttpConfig({timeout: abort.promise}).get();
abort.resolve();

To integrate it with your usecase, for example, you could have this in your service:

var abortGet;

this.getRecords = function(params) {
  ...
  if (abortGet) abortGet.resolve();
  abortGet = $q.defer();
  return Restangular
    .all('/api/records')
    .withHttpConfig({timeout: abortGet.promise})
    .post(filters);
}

This way calling getRecords always aborts the previous call if has not been resolved yet!

Method 2

This is another approach if you want to abort all http requests when change the state for UI-router:

angular
    .run(function(HttpHandlerSrv) {
        HttpHandlerSrv.abortAllRequestsOn('$stateChangeSuccess');
        HttpHandlerSrv.R.setFullRequestInterceptor(function(element, operation, route, url, headers, params, httpConfig) {
            httpConfig = httpConfig || {};
            if(httpConfig.timeout === undefined) {
                httpConfig.timeout = HttpHandlerSrv.newTimeout();
            }
            return { element: element, params: params, headers: headers, httpConfig: httpConfig };
        });
    })

    .factory('HttpHandlerSrv', HttpHandlerSrv);


    /** ngInject */
    function HttpHandlerSrv($q, $rootScope, Restangular) {
        var requests = [];

        return {
            R: Restangular,
            newTimeout: newTimeout,
            abortAllRequests: abortAllRequests,
            abortAllRequestsOn: abortAllRequestsOn
        };

        function newTimeout() {
            var request = $q.defer();
            requests.push(request);
            return request.promise;
        }

        function abortAllRequests() {
            angular.forEach(requests, function(request) {
                request.resolve();
            });
            requests = [];
        }

        function abortAllRequestsOn(event) {
            $rootScope.$on(event, function(event, newUrl, oldUrl) {
                if(newUrl !== oldUrl) { abortAllRequests(); }
            });
        }
    }

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply