javascript - 绑定(bind) uncaughtException 事件来处理回调 NodeJS 中发生的错误是最佳实践吗?

标签 javascript node.js

 var customError = function (){
    try{
        setTimeout(function(){
            console.log("time out called");
            throw new Error ("Error in call back");
        },5000);
    }catch(e){
        console.log(e);
    }
}
customError();

process.on('uncaughtException', function (err) {
  console.log(err);
})

我读了一些博客,发现我们可以使用“uncaughtException”过程事件来处理回调中发生的错误。但另一方面,我读到你应该使用 Promise 库而不是“uncaughtException”。但“uncaughtException”事件对我来说工作得很好。我只是想知道处理回调错误是否是最佳实践?

最佳答案

你不应该真正处理“uncaughtException”,因为你不知道它来自哪里(你的应用程序或 NodeJS)。

An unhandled exception means your application - and by extension Node.js itself - is in an undefined state. Blindly resuming means anything could happen.

https://nodejs.org/api/process.html#process_event_uncaughtexception

Joyent 有一篇关于处理 NodeJS 错误的好文章,地址:https://www.joyent.com/developers/node/design/errors

关于javascript - 绑定(bind) uncaughtException 事件来处理回调 NodeJS 中发生的错误是最佳实践吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33492737/

相关文章:

javascript - javascript 文件保存在哪里?

javascript - 类型 ="text/ecmascript"与类型 ="text/javascript"

javascript - AngularJS : Routing

Node.JS 异步加载模块

javascript - Gulp 连续生成图像

javascript - 在浏览器中使用浏览器化的 NodeJS 文件

javascript - sql语句中的数组不适用于占位符?nodejs

javascript - 当点击事件有子元素时不起作用

javascript - 如何在 jQuery 中定义条件属性选择器?

javascript - JS WebSocket 无法连接到运行 Node.js 'ws' 包的 AWS EC2 实例