I have an angular service that wraps my rest api calls and returns a $http promise.
My question is how do I throw an error so that a promise that triggers the .error method gets called? I don't want to just throw error since I want it to use the .success/.error in the calling function rather than doing a try catch block around it.
myFunction: function(foo)
if (foo) {
return $http.put(rootUrl + '/bar', {foo: foo});
}
else {
//what do I return here to trigger the .error promise in the calling function
}
You don't need $q.defer()
. And else
too. You can use reject directly:
myFunction: function(foo) {
if (foo) {
return $http.put(rootUrl + '/bar', {foo: foo});
}
return $q.reject("reject reason");
}
See https://docs.angularjs.org/api/ng/service/$q#reject
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