4

Oftentimes I want to call a promise and just let it run asynchronously instead of waiting for it. Like:

... some code ...

fetchMyCount().then(count => {
  updateTheUI(count);
});

... more code ...

Now this works great, but oftentimes I don't put a failure handler on the promise because that is pretty onerous for each one. That would be like putting a try {} catch {} on every one of my data fetches.

The problem is, if it fails, it does so silently. It doesn't throw an exception that my window.onerror can get, it doesn't log, it doesn't do anything.

Now I can make a nice wrapper like:

Async(fetchMycount().then(count => {
  updateTheUI(count);
}));

which can generically deal with promises, but then I have to remember to wrap each and every async call.

Is there a way to have a top-level handler like window.onerror or a big try {} catch {} wrapper that can catch all un-dealt-with rejected promises so I can log them and make sure they get fixed?

Paul Tarjan
  • 48,968
  • 59
  • 172
  • 213
  • Can you decorate the promise function? – Jorg Sep 16 '15 at 01:48
  • You are confusing rejections with errors. The two are not the same. A promise is just a way to resolve a value, and a rejection means that it couldn't be resolved. While I understand you'd like to catch all the rejections, because promises are chainable I don't think it's possible. It's like a function trying to know if the caller uses the return value or not. – Reactgular Sep 16 '15 at 02:18
  • You have to specify a rejection handler to handle rejected promises. There's no way around it. – Anid Monsur Sep 16 '15 at 02:29

2 Answers2

4

This is a common use case. Your problem is real. In Node, you have process.on("unhandledRejection", ... events which you can use. Sadly, these are not available in browsers yet. Some libraries which are compatible with ES2015 promises offer them and you can use those until native promises get support.

The upside is that support in browsers is coming and browsers will eventually support these hooks.

Currently, I've convinced the polyfill to add it, so if you're using the core-js polyfill you can do:

window.onunhandledrejection = function(e){
    // handle here e.promise e.reason
};

Note that this assumes that something is an unhandled rejection if: It's unhandled, and an error handler was not attached synchronously (within a task to be precise).

Community
  • 1
  • 1
Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
1

Exactly for issues like this, I wrote my own utility for writing asynchronous code:

https://github.com/vacuumlabs/yacol/

It does not help you with your existing code, however with newly written code it provides (among many other things) quite advanced error-handling capabilities.

This said, I don't think that "unhandledRejection" / "uncaughtException" events are good (although it's the best node.js natively provides) tools to do proper error handling:

  • First, you can only attach it process-wide, so it forces you to handle all uncaught errors the same way (unlike with try-catch where you can react differently to errors that originate from different parts of your code)

  • Second, "unhandledRejection" is called if .catch is not attached to the Promise in the same run of event loop. This may result in false errors being caught; note it is pretty OK to attach error handler to the promise later!

Tomas Kulich
  • 14,388
  • 4
  • 30
  • 35