Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit dc7f625

Browse files
gkalpakpetebacondarwin
authored andcommitted
refactor($http): clean up code
Closes #14921
1 parent 5db6257 commit dc7f625

File tree

1 file changed

+22
-14
lines changed

1 file changed

+22
-14
lines changed

src/ng/http.js

+22-14
Original file line numberDiff line numberDiff line change
@@ -959,26 +959,25 @@ function $HttpProvider() {
959959
config.paramSerializer = isString(config.paramSerializer) ?
960960
$injector.get(config.paramSerializer) : config.paramSerializer;
961961

962-
var chain = [serverRequest, undefined];
963-
var promise = initiateOutstandingRequest(config);
962+
$browser.$$incOutstandingRequestCount();
963+
964+
var requestInterceptors = [];
965+
var responseInterceptors = [];
966+
var promise = $q.when(config);
964967

965968
// apply interceptors
966969
forEach(reversedInterceptors, function(interceptor) {
967970
if (interceptor.request || interceptor.requestError) {
968-
chain.unshift(interceptor.request, interceptor.requestError);
971+
requestInterceptors.unshift(interceptor.request, interceptor.requestError);
969972
}
970973
if (interceptor.response || interceptor.responseError) {
971-
chain.push(interceptor.response, interceptor.responseError);
974+
responseInterceptors.push(interceptor.response, interceptor.responseError);
972975
}
973976
});
974977

975-
while (chain.length) {
976-
var thenFn = chain.shift();
977-
var rejectFn = chain.shift();
978-
979-
promise = promise.then(thenFn, rejectFn);
980-
}
981-
978+
promise = chainInterceptors(promise, requestInterceptors);
979+
promise = promise.then(serverRequest);
980+
promise = chainInterceptors(promise, responseInterceptors);
982981
promise.finally(completeOutstandingRequest);
983982

984983
if (useLegacyPromise) {
@@ -1006,9 +1005,18 @@ function $HttpProvider() {
10061005

10071006
return promise;
10081007

1009-
function initiateOutstandingRequest(config) {
1010-
$browser.$$incOutstandingRequestCount();
1011-
return $q.when(config);
1008+
1009+
function chainInterceptors(promise, interceptors) {
1010+
for (var i = 0, ii = interceptors.length; i < ii;) {
1011+
var thenFn = interceptors[i++];
1012+
var rejectFn = interceptors[i++];
1013+
1014+
promise = promise.then(thenFn, rejectFn);
1015+
}
1016+
1017+
interceptors.length = 0;
1018+
1019+
return promise;
10121020
}
10131021

10141022
function completeOutstandingRequest() {

0 commit comments

Comments
 (0)