Reputation: 8680
I'm using Restangular to get data by chained promises. Here's a simplified example of what I'm doing:
Restangular.one("employer").get().then(function(employer) {
return employer;
}, function(response) {
//response.status === '401'
$location(login);
}).then(function(company) {
Restangular.one("reviews?company_name='" + company).get()
.then(function(reviews) {
$scope.reviews = reviews.originalElement;
});
});
First I need to get the user's employer and then the reviews attached to this employer. I think it works fine until I get a 401 response back from the server.
When it happens, I send the user to the login screen. This also works fine, but the problem is that the whole chain gets executed – which will return in one more 401.
Ultimately I'd like the chain to stop when the error occurs. I could probably do it by throwing an error, but is that really a good solution? Am I approaching this in the wrong way?
Upvotes: 4
Views: 1266
Reputation: 34347
In your error handler, you need to return false;
to stop the promise chain:
Restangular.one("employer").get().then(function(employer) {
return employer;
}, function(response) {
//response.status === '401'
$location(login);
return false
}).then(function(company) {
Restangular.one("reviews?company_name='" + company).get()
.then(function(reviews) {
$scope.reviews = reviews.originalElement;
});
});
Upvotes: 3