Reputation: 100130
I am remember researching this awhile back and coming up empty handed and I still can't find any good info on this - why does there appear to be a $q.reject
method but not an $http.reject
method?
For example, in real life we might have:
unfollow: function (userId) {
if (!AuthService.isLoggedIn()) {
//$location.url('/login');
window.location.href = '/login';
return $q.reject({error: 'no logged-in user, but non-existent user could still click a follow button?'});
}
else {
return $http({
method: 'PUT',
url: ConfigService.api.baseUrl + '/v1/users/add_unfollow/by_id/' + userId
});
}
}
I would rather uses the relevant $http.reject
instead of $q.reject
, but that doesn't seem to work.
Upvotes: 0
Views: 571
Reputation: 6054
$http would wrap the http calls and return a promise. The promise would rejected if the actual http request is failed, and adding a reject method would not make much sense, rather it should be in the promise.
In your example you would not even need to call the $http service to reject the request.
Upvotes: 1
Reputation: 171679
Since $http
returns a promise in one leg of the conditional...the function itself needs to return a promise in the other leg also.
Using $q.reject()
is simply a shortcut to return a rejected promise.
Without it, any place that calls unfollow().then()
wouldn't have a then()
method if a promise wasn't returned
Upvotes: 1