AngularJS: rejecting a promise
I am doing Factory
in AngularJS, 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, all I have to do is call the promise $http
, so that means whoever is using my factory is expecting a promise. The point is, if the condition is false, I need to return the rejected promise. Is there a way to do this?
Thank!
+3
source to share
1 answer
Return $q.reject()
which rejected the object's promises:
if (href) {
return $http({ method: method, url: item.href });
}
else {
return $q.reject();
}
You can also indicate the reason for the rejection, i.e. $q.reject('Invalid URL')
...
+5
source to share