Recently, I encountered an angular Service containing a crucial function:
service.getItemByID = function(id) {
var hp = $http({method: "GET", url: "service/open/item/id",
headers: {"token": $rootScope.user.token},
params: {"id": id}});
return hp;
};
The challenge at hand involved manipulating the returned values before passing them further while maintaining the structure of HttpPromise to ensure compatibility with the existing controller code that relies on the success and failure functions of the HttpPromise.
In order to address this, I revamped the service code as follows:
service.getItemByID = function(id) {
var hp = $http({method: "GET", url: "service/open/item/id",
headers: {"token": $rootScope.user.token},
params: {"id": id}});
var newHP = hp.success(
function(data, status, headers, config) {
data.x = "test"; //TODO: add full manipulation
alert("success");
return hp;
});
return newHP;
};
After implementation, this revised code proved effective regardless of returning hp or newHP. Thus, the question arises: Is this method of HttpPromise chaining considered appropriate?