Skip to content

Instantly share code, notes, and snippets.

@BrianGenisio
Last active December 21, 2015 11:29
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save BrianGenisio/6299303 to your computer and use it in GitHub Desktop.
Save BrianGenisio/6299303 to your computer and use it in GitHub Desktop.
The default behavior is to reject the error. If you don't do that, you are hiding the error condition.
angular.module('httpProgress')
.factory('httpProgressInterceptor', function($q) {
var requestCount = 0;
function updateProgress(update) {
var previous = requestCount;
requestCount += update;
if(previous <= 0 && requestCount > 0) {
NProgress.start();
} else if (previous > 0 && requestCount <= 0) {
NProgress.done(true);
} else {
NProgress.inc();
}
}
function incrementRequests() {
updateProgress(1);
}
function decrementRequests() {
updateProgress(-1);
}
return {
request: function (request) {
incrementRequests();
return request;
},
response: function (response) {
decrementRequests();
return response;
},
responseError: function (rejection) {
decrementRequests();
return $q.reject(rejection);
}
};
})
.config(function ($httpProvider) {
$httpProvider.interceptors.push('httpProgressInterceptor');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment