So I know that when calling success or error on an $http
result, it will be called on the $http
promise and then the original one will be returned rather than the updated one. What I truly don't understand is why?!
Currently, you can write something like:
$http(config)
.success(function(data) { console.log(data); return 1; })
.then(function (response) {
var msg = 'Hey, I am the original response, not 1, ';
msg += 'and I can run even before success is completed. ';
msg += 'This is nearly fake chaining...';
console.log(msg);
});
More coding style, is there a good reason not to replace the code here by this one?
// The util method has been put after the return
// just as the other $http internal methods
return decoratePromise(promise);
// Util method to add method 'success' and 'error'
// to a promise. This will spread the raw respons
function decoratePromise(p) {
p.success = function(fn) {
return decoratePromise(p.then(function(response) {
return fn(response.data, response.status, response.headers, config);
}));
};
p.error = function(fn) {
return decoratePromise(p.then(null, function(response) {
return fn(response.data, response.status, response.headers, config);
}));
};
return p;
}
I don't really know what to think about those two methods... Is there a good reason to use them regarding this limitation?
Thanks for any info!