Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the order of execution of $http interceptors in AngularJS?

Tags:

angularjs

Suppose I registered several HTTP interceptors in angular.js app:

  $httpProvider.interceptors.push(function() {
    return {
      request: function(config) {
        console.log("interceptor A request");
        return config;
      },
      response: function(res) {
        console.log("interceptor A response");
        return res;
      }
    };
  });
  $httpProvider.interceptors.push(...); // interceptor B with similar code
  $httpProvider.interceptors.push(...); // interceptor C with similar code

In which order will they be executed?

like image 209
jakub.g Avatar asked Dec 14 '22 07:12

jakub.g


1 Answers

It seems that the interceptors are executed:

  • in registration order for requests
  • in reverse registration order for responses

Log:

interceptor A request
interceptor B request
interceptor C request
(request happens here)
interceptor C response
interceptor B response
interceptor A response
like image 171
jakub.g Avatar answered Dec 16 '22 21:12

jakub.g