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!
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').
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With