问题
Is there a way to configure Q to log or call a specific function on all rejected promises (like an interceptor)?
Many exceptions are being swallowed in my application, and put error handling in all my promises just for logging purposes would be duplicated work to do.
Thanks!
回答1:
Q actually already supports this - as of 1.3.0 Q offers the standard unhandled rejection hooks:
process.on("unhandledRejection", function(reason, p) {
console.log("Unhandled rejection detected ", reason, p);
});
You can also log caught errors from .done with Q.onerror:
Q.onerror = function(error){
// errors will be here and not thrown in `done` chains.
};
来源:https://stackoverflow.com/questions/30830688/log-all-reject-promises-in-q