Is it possible to have an interceptor wait for some event before sending the request to the server?
Here is the code of the interceptor:
(function() {
"use strict";
angular.module("fuse").config(config);
/** @ngInject */
function config($httpProvider, $provide) {
$provide.factory("httpInterceptor", function($q, $rootScope) {
return {
request: function(config) {
// I need to wait for some event, read its argument and add this argument to the headers
return config || $q.when(config); // Only return after handling the event
},
response: function(response) {
console.log("Data Count: " + response.data.length);
return response || $q.when(response);
}
};
});
$httpProvider.interceptors.push("httpInterceptor");
}
})();
As you can see, before returning the config object, i want to handle some event that contains additional data that I need to add to the headers.
Is it at all possible?
Yes, you can do that,return your promise
var deferred = $q.defer();
, than when async method finish, you can resolve your updated config :More you can read in AngularJS Interceptors