Reputation: 2143
I'm making a Factory
in AngularJS which is something like this:
if (href) {
return $http({ method: method, url: item.href });
}
else {
// needs to return a failed promise
}
So, if the condition is true, everything is I need to do is to invoke the $http
promise, so it means that who is using my factory is expecting a promise. The thing is: if the condition is false, I need to return a rejected promise. Is there a way to do that?
Thanks!
Upvotes: 3
Views: 416
Reputation: 193271
Return $q.reject()
which is rejected promise object:
if (href) {
return $http({ method: method, url: item.href });
}
else {
return $q.reject();
}
You can also specify a rejection reason, i.e. $q.reject('Invalid URL')
.
Upvotes: 5