Skip to content

Instantly share code, notes, and snippets.

@c0bra
Created May 16, 2013 20:30
Show Gist options
  • Save c0bra/5594851 to your computer and use it in GitHub Desktop.
Save c0bra/5594851 to your computer and use it in GitHub Desktop.
var chain = [serverRequest, undefined];
var dummyP = $q.defer();
var promise = dummyP.promise; // $q.when(config);
// apply interceptors
forEach(reversedInterceptors, function(interceptor) {
if (interceptor.request || interceptor.requestError) {
chain.unshift(interceptor.request, interceptor.requestError);
}
if (interceptor.response || interceptor.responseError) {
chain.push(interceptor.response, interceptor.responseError);
}
});
while(chain.length) {
var thenFn = chain.shift();
var rejectFn = chain.shift();
promise = promise.then(thenFn, rejectFn);
};
promise.success = function(fn) {
promise.then(function(response) {
fn(response.data, response.status, response.headers, config);
});
return promise;
};
promise.error = function(fn) {
promise.then(null, function(response) {
fn(response.data, response.status, response.headers, config);
});
return promise;
};
$timeout(function() {
dummyP.resolve(config);
});
return promise;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment