How to deal with dangling promises

2020-02-29 03:48发布

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?

2条回答
贪生不怕死
2楼-- · 2020-02-29 04:31

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!

查看更多
SAY GOODBYE
3楼-- · 2020-02-29 04:33

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).

查看更多
登录 后发表回答