Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular's $q.reject() vs deferred.reject()

I'm trying to get a handle on the Angular $q service and its related objects and APIs. When I look at the objects in my console I see:

var deferred = $q.defer()  ...(and then from console inspection)...  $q: Object {defer: function, reject: function, when: function, all: function}  deferred: Object {resolve: function, reject: function, notify: function, promise: Object}  deferred.promise: Object {then: function, catch: function, finally: function} 

It raises a few questions:

  1. What's the difference between $q.reject() and deferred.reject()? When to use each?
  2. What's the relationship between the errorFn in deferred.promise.then(successFn, errorFn) and the catchFn in deferred.promise.catch(catchFn)?
  3. If I have a bunch of nested promises and an error occurs, will the outermost catch() function always be called? What if one of the nested promises also has a catch function defined? Will that catch prevent the outermost catch from executing?

Thanks.

like image 918
lostdorje Avatar asked Jun 27 '14 04:06

lostdorje


People also ask

What is Q defer ()?

$q. defer() allows you to create a promise object which you might want to return to the function that called your login function.

What does $q do in AngularJS?

$q is an angular defined service. It's the same as new Promise(). But $q takes things to the next level by enhancing additional feature that developers can use to perform complex tasks more simply. resolve(value) – resolves the derived promise with the value.

What does promise return in angular?

What Is Promise in Angular? Promises in Angular provide an easy way to execute asynchronous functions that use callbacks, while emitting and completing (resolving or rejecting) one value at a time. When using an Angular Promise, you are enabled to emit a single event from the API.


2 Answers

1) $q.reject() is a shortcut to create a deferred and then reject it immediately; I often use this in an errorFn if I can't handle the error.

2) Nothing, promise.catch(errorFn) is just syntactic sugar for promise.then(null, errorFn), just like the success and error methods of the $http service, so you can write code like the following:

promise.     then(function(result){         // handle success         return result;     }, function errorHandler1(error){         // handle error, exactly as if this was a separate catch in the chain.      }).catch(function errorHandler2(error){         // handle errors from errorHandler1     }); 

3) This is exactly where $q.reject can come in handy:

promise.     catch(function(error){        //Decide you can't handle the error        return $q.reject(error); //This forwards the error to the next error handler;     }).catch(function(error){        // Here you may handle the error or reject it again.        return 'An error occurred';         //Now other errorFn in the promise chain won't be called,         // but the successFn calls will.     }).catch(function(error){        // This will never be called because the previous catch handles all errors.     }).then(function(result){        //This will always be called with either the result of promise if it was successful, or         //'An error occured' if it wasn't     }); 
like image 92
dustyrockpyle Avatar answered Sep 22 '22 23:09

dustyrockpyle


Ok this is my take from promises.

  1. $q.reject(reason) returns a rejected promise with the reason passed as argument and defered. Reject rejects an existent defered whether its process has finished or not.

  2. errorFn gets launched when a promise is rejected and its argument is the reason why it was rejected. Catch is called when an error inside the promise process wasn't handled properly causeing the promise to raise and exception and not being either rejected or fulfilled .

  3. You shoudn't have nested promises, you should have chained promises in which case the latest catch block should catch everything prior to it if no other block was specified to handle it.

like image 29
Dayan Moreno Leon Avatar answered Sep 24 '22 23:09

Dayan Moreno Leon