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

I am trying to get an Angular $q service descriptor and its associated objects and API. When I look at objects in the 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} 

There are several questions:

  • What is the difference between $q.reject() and deferred.reject() ? When to use each?
  • What is the relationship between errorFn in deferred.promise.then(successFn, errorFn) and catchFn in deferred.promise.catch(catchFn) ?
  • If I have a bunch of nested promises and an error occurs, will the most external catch() function always be called? What if one of the nested promises also has a catch function? Will this catch prevent the outermost catch from being fulfilled?

Thank.

+58
javascript angularjs promise
Jun 27 '14 at 4:02
source share
2 answers

1) $q.reject() is a shortcut for creating a pending one and then immediately rejects it; I often use this in errorFn if I cannot handle the error.

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

 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 }); 
+86
Jun 27 '14 at 4:34
source share

Ok, this is my request from promises.

  • $q.reject(reason) returns a rejected promise with a reason passed as an argument and deferred. Rejection rejects the existing deferred whether its process is completed or not.

  • errorFn triggered when a promise is rejected, and its argument is the reason for its failure. Catch is called when an error within the promise process is not handled properly, causing the promise to raise and to exclude, and not be rejected or executed.

  • You do not have nested promises, you must have a chain of promises, in which case the last catch block should catch everything before it has been processed.

+4
Jun 27 '14 at 4:32
source share



All Articles