How to know if all $http calls are over - AngularJS

InquisitiveP

I need to trigger an event when all of my $http calls are processed. I also need to know if any call has failed. I tried using the available solutions on stackoverflow such as using an interceptor.

angular.module('app').factory('httpInterceptor', ['$q', '$rootScope',
  function ($q, $rootScope) {
    var loadingCount = 0;

    return {
      request: function (config) {
        if(++loadingCount === 1) {
          $rootScope.$broadcast('loading:progress');
        }
        return config || $q.when(config);
      },    
      response: function (response) {
        if(--loadingCount === 0) {
          $rootScope.$broadcast('loading:finish');
        }
        return response || $q.when(response);
      },    
      responseError: function (response) {
        if(--loadingCount === 0) {
          $rootScope.$broadcast('loading:finish');
        }
        return $q.reject(response);
      }
    };
  }
]).config(['$httpProvider', function ($httpProvider) {
  $httpProvider.interceptors.push('httpInterceptor');
}]);

However, with this approach the $rootScope.$broadcast('loading:finish') gets called after every $http call is completed. I want to trigger an event when all the $http calls are over.

I cannot use $q since the $http calls in my page belong to various directives and are not called in the same controller.

schaturv

You can use below code to check the number of pending requests for $http. I am using this to show loading spinner in my project.

$http.pendingRequests.length

For keeping a track of failed and success calls you can use something like this:

angular.module('myApp', [])
.run(function ($rootScope){
  $rootScope.failedCalls = 0;
  $rootScope.successCalls = 0;
 })
.controller('MyCtrl', 
function($log, $scope, myService) {
 $scope.getMyListing = function(employee) {
   var promise = 
       myService.getEmployeeDetails('employees');
   promise.then(
      function(payload) { 
          $scope.listingData = payload.data;
          $rootScope.successCalls++; //Counter for success calls
      },
      function(errorPayload) {
        $log.error('failure loading employee details', errorPayload);
        $rootScope.failedCalls++; //Counter for failed calls
      });
 };
 })
 .factory('myService', function($http) {
  return {
  getEmployeeDetails: function(id) {
     return $http.get('/api/v1/employees/' + id);
  }
}
 });

Basically I have created 2 root scope variables and using it as counters to maintain count of success and failed calls which you can use wherever you want.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How To Know All Asynchronous HTTP Calls are Completed

How to know when all Angular2 HTTP calls are finished

How to manage AngularJS http requests calls

how to chain two http calls in AngularJS?

q.all() making duplicate ajax calls (Angularjs, $http, $q)

AngularJS $q.all - wait between http calls

using $q.all for making synchronous http calls not working angularjs

AngularJs directive and $http calls

Multiple $http calls in AngularJS

How to know my website is available all over the world?

How do I stop all ongoing Ajax calls in AngularJS

AngularJS + Jasmine: Unexpected http calls

HTTP Post calls sequentially Angularjs

AngularJs : Multiple Calls for $http service

How to add custom HTTP header to all AJAX calls in BackboneJS models?

How to intercept/log all incoming/outgoing HTTP calls in a Web API

HTTP Client Programming - How to know server have sent all the datas

How do I know if my website is being served over HTTP or HTTP/2?

AngularJS: How to disable default watchers while getting data over $http

how to know scope in angularjs

Java EJB @Asynchronous loop - How to know when all the calls are complete before moving on?

How do I know when all AJAX calls from a particular functions are finished?

How can I run a callback when I know all asynchronous calls have been completed?

make multiple http calls synchronously in AngularJs

setting HTTP Header for angularjs REST calls

Conditional Chaining of $http-calls with Promises in AngularJS

multipe $http calls inside a controller in angularjs

How to know if asynchronous calls are finished? (HackerNews Comments)

How can I return a promise from a function that calls a $http.get in AngularJS?