[英]NodeJS. Can I handle errors in one place? Without try and catch blocks
Node said that I should catch errors on all my promises. Node表示我应该兑现所有诺言。
Does exist a good practice for handling the same errors in one function? 是否存在在一个函数中处理相同错误的良好实践? I don't want to write always the same catch block for all requests.
我不想为所有请求编写始终相同的catch块。
My code looks like: 我的代码如下:
(async () => {
try {
await makeRequest()
}
catch(e) {
console.error(e)
}
})()
Try this : 尝试这个 :
makeRequest()
.catch(err => {
// console.error(err)
})
For async
IIFE (IIAFE) error handling is performed as: 对于
async
IIFE(IIAFE),错误处理如下:
(async () => {
await makeRequest()
})().catch(console.error);
Notice that in Node.js console
methods are bound to proper context, so they can be passed as callbacks. 请注意,在Node.js中,
console
方法绑定到适当的上下文,因此可以将它们作为回调传递。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.