Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Promises and generic .catch() statements

I'm writing an API for my system, that's sending an XHR to the server and returns a promise that should be handled by the caller - so far so good.

For each API call I must use a .then and .catch calls, but usually (like 75% of the time) the .catch references the same functionality which simply prints using console.error.

My question is - Is there a way to attach a default catch statement for each promise that I create? (that let's say prints to the console), and for each promise that I would like to further handle the rejection, I would add another .catch call (or even override it)?

Simplified example where each call has its own .catch: http://jsbin.com/waqufapide/edit?js,console

Non working version that tries to implement the desired behavior: http://jsbin.com/nogidugiso/2/edit?js,console

In the second example, instead of just returning deferred.promise, I return a promise with an attached catch() handler:

return deferred.promise.catch(function (error) {
  console.error(error);
});

Both then catch and then functions are called in that case.

I do realize the Q exposes the getUnhandledReasons() function and onerror event, but I don't really want to use .done() for each promise nor build some kind of timer/interval to handle list of un-handled rejections.

Other libraries such as bluebird expose onPossiblyUnhandledRejection events, which I have to admit is a bit nicer solution, but still not quite what I'm looking for.

like image 926
Gilad Artzi Avatar asked Jul 11 '15 07:07

Gilad Artzi


People also ask

How do you use a catch with a Promise?

In summary: then : when a promise is successful, you can then use the resolved data. 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.

What is catch in Promise?

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

Is then () a Promise?

The then() method returns a Promise . It takes up to two arguments: callback functions for the success and failure cases of the Promise .

What are the promises in JavaScript?

The Promise object represents the eventual completion (or failure) of an asynchronous operation and its resulting value. To learn about the way promises work and how you can use them, we advise you to read Using promises first.


2 Answers

I think all you want to do is rethrow the exception after you've logged it so other handlers will deal with it properly:

return deferred.promise.catch(function (error) {
  console.error(error);
  throw e; // rethrow the promise
});
like image 53
Benjamin Gruenbaum Avatar answered Sep 17 '22 05:09

Benjamin Gruenbaum


Q use NodeJS process to raise unhandledRejection. If you dont use NodeJS, you can use this Workaround:

// Simulating NodeJS process.emit to handle Q exceptions globally
process = {
    emit: function (event, reason, promise) {
        switch(event)
        {
            case 'unhandledRejection':
                console.error("EXCEPTION-Q> %s", reason.stack || reason)
                break;
        }
    }
}
like image 40
Alejandro Tamayo Avatar answered Sep 19 '22 05:09

Alejandro Tamayo