Skip to content

Instantly share code, notes, and snippets.

@jimschubert
Created October 29, 2014 15:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jimschubert/02d45b63c0d79c0afd32 to your computer and use it in GitHub Desktop.
Save jimschubert/02d45b63c0d79c0afd32 to your computer and use it in GitHub Desktop.
Tweaking $http response without a response interceptor (AngularJS)
function makeRequest(){
// var config = {...}
var query = $http(config);
query.then(function onSuccessIntermediate(response) {
// When this HTTP response comes back, we will use this function to tweak any data
var deferred = $q.defer();
var promise = deferred.promise;
// $http has a success and error helper function, so we need to make this promise look the same.
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;
};
if (angular.isObject(response) && angular.isObject(response.data)) {
// Modify response.data, then resolve
// response.data.touched = 1;
deferred.resolve(response);
}
return promise;
});
return query;
}
/*
This can be used just like $http:
makeRequest()
.success(function(data){})
.error(function(data){});
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment