As someone who is still getting the hang of Angular and promises, I want to make sure I'm on the right track.
Right now, my data layer service uses Restangular to fetch data and returns a promise. Here's how it looks...
dataStore.getUsers = function (params) {
return users.getList(params);
};
The controller that calls this function receives a promise in response like this...
$dataStore.getUsers(params).then(function (response) {
$scope.users = response;
}, function(response) {
$log.error("There was an error fetching users: ", response);
});
While this setup is working fine, I want to handle the promise within my datastore before sending it back. I'd like to use the .then() method to check for errors and log them. Then, from both the success and failure functions, I want to return the original promise back to the controller.
I don't want to change any code in my controller; I just want to modify my datastore code. Here's some simplified code to illustrate what I'm aiming for...
dataStore.getUsers = function (params) {
users.getList(params).then(function (response) {
$log("Server responded")
return original promise;
}, function(response) {
$log.error("Server did not respond");
return original promise;
});
};