Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Promises - error callback vs. catch

Can somebody tell me if there is a difference between using an error callback vs. a catch function, when using $q.promise please?

E.g. are the two snippets of code functionally equivalent?

function doSomething0() {
    var deferred = $q.defer();

    ...

    return deferred.promise;
 }

 doSomething0()
    .then(doSomething1)
    .then(doSomething2)
    .then(doSomething3)
    .catch(function (err) {
        // do something with `err`
    });

vs.

function doSomething0() {
    var deferred = $q.defer();

    ...

    return deferred.promise;
 }

 function errorHandler(err) {
    // do something with `err`
 }

 doSomething0()
    .then(doSomething1, errorHandler)
    .then(doSomething2, errorHandler)
    .then(doSomething3, errorHandler);

If so, why use the second one? It looks far uglier and leads to more code duplication in my opinion?

like image 986
keldar Avatar asked Sep 23 '15 16:09

keldar


People also ask

Is catch a promise?

catch() The catch() method returns a Promise and deals with rejected cases only. It behaves the same as calling Promise.

Does try catch work on promise?

The "invisible try.. catch " around the executor automatically catches the error and turns it into rejected promise. This happens not only in the executor function, but in its handlers as well. If we throw inside a .

What is the advantage of promise over callback?

They can handle multiple asynchronous operations easily and provide better error handling than callbacks and events. In other words also, we may say that, promises are the ideal choice for handling multiple callbacks at the same time, thus avoiding the undesired callback hell situation.

How does catch work in promises?

catch : when a promise fails, you catch the error, and do something with the error information. finally : when a promise settles (fails or passes), you can finally do something.


2 Answers

Both will achieve the same thing, except the second one might run errorHandler three times (instead of just once). You are correct that it brings some code duplication, but it also allows you to treat whatever error happened and continue with your chain:

function errorHandler(err) {
  //log error, continue
  return $q.resolve('default value or something');
}

doSomething0()
  .then(doSomething1, errorHandler)
  .then(doSomething2, errorHandler)
  .then(doSomething3, errorHandler);
like image 55
Marcelo Avatar answered Sep 22 '22 02:09

Marcelo


Let's have a look at the first sample:

doSomething0()
    .then(doSomething1, errorHandler)
    .then(doSomething2, errorHandler)
    .then(doSomething3, errorHandler);


// I've represented functions invocations as if they were synchronous to simplify the example to focus on the error handling
// The above sample is in a way "equivalent" to
try {
    // doSomething0()
    try {
        // doSomething1()
        try {
          // doSomething2()
        } catch(e0) {
           // handle error
        }  
    } catch(e1) {
         // handle error
    }
} catch(e2) {
     // handle error
}
// doSomething3()

But if an exception happens in the doSomething3 handler, it won't be handled.

Ok, let's have a look at the second sample:

doSomething0()
    .then(doSomething1)
    .then(doSomething2)
    .then(doSomething3)
    .catch(function (err) {
        // do something with `err`
    });


// I've represented functions invocations as if they were synchronous to simplify the example to focus on the error handling
// The above sample is in a way "equivalent" to
try {
    // doSomething0()
    // doSomething1()
    // doSomething2()
    // doSomething3()
}
catch(e) {
    // Catch 'em all
    console.log(e)
}
like image 41
AlexMelw Avatar answered Sep 19 '22 02:09

AlexMelw