forked from mamluka/angular-promise-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
promise-tracker-http-interceptor.js
47 lines (43 loc) · 1.47 KB
/
promise-tracker-http-interceptor.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*
* promise-tracker - v2.1.0 - 2014-11-15
* http://github.com/ajoslin/angular-promise-tracker
* Created by Andy Joslin; Licensed under Public Domain
*/
(function() {
angular.module('ajoslin.promise-tracker')
.config(['$httpProvider', function($httpProvider) {
$httpProvider.interceptors.push(['$q', 'promiseTracker', function($q, promiseTracker) {
return {
request: function(config) {
if (config.tracker) {
if (!angular.isArray(config.tracker)) {
config.tracker = [config.tracker];
}
config.$promiseTrackerDeferred = config.$promiseTrackerDeferred || [];
angular.forEach(config.tracker, function(tracker) {
var deferred = tracker.createPromise();
config.$promiseTrackerDeferred.push(deferred);
});
}
return $q.when(config);
},
response: function(response) {
if (response.config && response.config.$promiseTrackerDeferred) {
angular.forEach(response.config.$promiseTrackerDeferred, function(deferred) {
deferred.resolve(response);
});
}
return $q.when(response);
},
responseError: function(response) {
if (response.config && response.config.$promiseTrackerDeferred) {
angular.forEach(response.config.$promiseTrackerDeferred, function(deferred) {
deferred.reject(response);
});
}
return $q.reject(response);
}
};
}]);
}]);
}());