Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS: return promise reject

Tags:

angularjs

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!

like image 855
Matheus Lima Avatar asked Jul 21 '26 19:07

Matheus Lima


1 Answers

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').

like image 102
dfsq Avatar answered Jul 24 '26 15:07

dfsq



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!