javascript - How to deal with dangling promises - Stack Overflow

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

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?

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?

Share Improve this question edited Sep 16, 2015 at 7:01 Paul Tarjan asked Sep 16, 2015 at 1:41 Paul TarjanPaul Tarjan 50.7k59 gold badges176 silver badges214 bronze badges 3
  • Can you decorate the promise function? – Jorg Commented Sep 16, 2015 at 1: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 Commented Sep 16, 2015 at 2:18
  • You have to specify a rejection handler to handle rejected promises. There's no way around it. – Anid Monsur Commented Sep 16, 2015 at 2:29
Add a ment  | 

2 Answers 2

Reset to default 4

This is a mon 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 patible with ES2015 promises offer them and you can use those until native promises get support.

The upside is that support in browsers is ing 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).

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

https://github./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!

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744938740a4602188.html

相关推荐

  • javascript - How to deal with dangling promises - Stack Overflow

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

    1天前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信