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

Commit 9d2de34

Browse files
committed
refactor($http): clean up code
Closes #14921
1 parent 9c0659c commit 9d2de34

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

src/ng/http.js

+22-10
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,7 @@ function $HttpProvider() {
586586
*
587587
* ### Overriding the Default Transformations Per Request
588588
*
589-
* If you wish override the request/response transformations only for a single request then provide
589+
* If you wish to override the request/response transformations only for a single request then provide
590590
* `transformRequest` and/or `transformResponse` properties on the configuration object passed
591591
* into `$http`.
592592
*
@@ -959,25 +959,23 @@ function $HttpProvider() {
959959
config.paramSerializer = isString(config.paramSerializer) ?
960960
$injector.get(config.paramSerializer) : config.paramSerializer;
961961

962-
var chain = [serverRequest, undefined];
962+
var requestInterceptors = [];
963+
var responseInterceptors = [];
963964
var promise = $q.when(config);
964965

965966
// apply interceptors
966967
forEach(reversedInterceptors, function(interceptor) {
967968
if (interceptor.request || interceptor.requestError) {
968-
chain.unshift(interceptor.request, interceptor.requestError);
969+
requestInterceptors.unshift(interceptor.request, interceptor.requestError);
969970
}
970971
if (interceptor.response || interceptor.responseError) {
971-
chain.push(interceptor.response, interceptor.responseError);
972+
responseInterceptors.push(interceptor.response, interceptor.responseError);
972973
}
973974
});
974975

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

982980
if (useLegacyPromise) {
983981
promise.success = function(fn) {
@@ -1004,6 +1002,20 @@ function $HttpProvider() {
10041002

10051003
return promise;
10061004

1005+
1006+
function chainInterceptors(promise, interceptors) {
1007+
for (var i = 0, ii = interceptors.length; i < ii;) {
1008+
var thenFn = interceptors[i++];
1009+
var rejectFn = interceptors[i++];
1010+
1011+
promise = promise.then(thenFn, rejectFn);
1012+
}
1013+
1014+
interceptors.length = 0;
1015+
1016+
return promise;
1017+
}
1018+
10071019
function executeHeaderFns(headers, config) {
10081020
var headerContent, processedHeaders = {};
10091021

0 commit comments

Comments
 (0)