Skip to content

Commit 78932bd

Browse files
committed
refactor($http): clean up code
1 parent 3c8b3cc commit 78932bd

File tree

1 file changed

+23
-14
lines changed

1 file changed

+23
-14
lines changed

src/ng/http.js

+23-14
Original file line numberDiff line numberDiff line change
@@ -947,6 +947,7 @@ function $HttpProvider() {
947947
throw minErr('$http')('badreq', 'Http request configuration url must be a string. Received: {0}', requestConfig.url);
948948
}
949949

950+
950951
var config = extend({
951952
method: 'get',
952953
transformRequest: defaults.transformRequest,
@@ -959,26 +960,25 @@ function $HttpProvider() {
959960
config.paramSerializer = isString(config.paramSerializer) ?
960961
$injector.get(config.paramSerializer) : config.paramSerializer;
961962

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

965969
// apply interceptors
966970
forEach(reversedInterceptors, function(interceptor) {
967971
if (interceptor.request || interceptor.requestError) {
968-
chain.unshift(interceptor.request, interceptor.requestError);
972+
requestInterceptors.unshift(interceptor.request, interceptor.requestError);
969973
}
970974
if (interceptor.response || interceptor.responseError) {
971-
chain.push(interceptor.response, interceptor.responseError);
975+
responseInterceptors.push(interceptor.response, interceptor.responseError);
972976
}
973977
});
974978

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

984984
if (useLegacyPromise) {
@@ -1006,9 +1006,18 @@ function $HttpProvider() {
10061006

10071007
return promise;
10081008

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

10141023
function completeOutstandingRequest() {

0 commit comments

Comments
 (0)