Matheus Lima
Matheus Lima

Reputation: 2143

AngularJS: return promise reject

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

Answers (1)

dfsq
dfsq

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

Related Questions