I have a function called getData that retrieves data from an API endpoint.
My current implementation utilizes $q to handle promises. After processing the retrieved data, I return another promise:
var getData = function (controller) {
var defer = $q.defer();
$http.get('/api/' + controller + '/GetData')
.success(function (data) {
var modifiedData = [{ id: 0, name: '*' }].concat(data);
defer.resolve({
originalData: data,
modifiedData: modifiedData
});
})
.error(function (error) {
defer.reject({
errorData: error
});
});
return defer.promise;
}
I'm curious if there's an alternate way to achieve this without relying on AngularJS's $q or any other similar implementation. Is the code above the most efficient approach, considering that I do not want to pass onSuccess and onError as parameters to the getData function?
Thanks!