I'm reading AngularJS Up and Running. In chapter 6, it mentions handing errors from promises like this:
$http.get('/api/server-config').then(function(configResponse) {
return $http.get('/api/' + configResponse.data.USER_END_POINT);
}).then(function(userResponse) {
return $http.get('/api/' + userResponse.data.id + '/items');
}).then(function(itemResponse) {
// Display items here
}, function(error) {
// Common error handling
});
And in other places, I see .catch()
being used (for example, the answer here: Assigning variable from a factory to a control doesn't work uses .catch()
like so:
BaseService.fetch.stuffs
.then(function(data) {
self.stuffies = data;
console.log(self.stuffies);
}).catch(function(errorResponse) {
self.cerrorMessages = errorResponse.data;
});
My question is, what's the difference between the above menthod and the method shown in the book:
BaseService.fetch.stuffs
.then(function(data) {
self.stuffies = data;
console.log(self.stuffies);
}, function(error) {
self.cerrorMessages = errorResponse.data;
});
What's preferred?