gpt4 book ai didi

javascript - TJ 的告别帖子中 Node.js 错误处理中的 "not get a callback at all"是什么意思?

转载 作者:搜寻专家 更新时间:2023-10-31 23:35:36 26 4
gpt4 key购买 nike

最近看了TJ的博文:"Farewell Node.js" .

我不太了解 Node 失败部分。在这里:

Error-handling in Go is superior in my opinion. Node is great in the sense that you have to think about every error, and decide what to do. Node fails however because:

  • you may get duplicate callbacks
  • you may not get a callback at all (lost in limbo)
  • you may get out-of-band errors
  • emitters may get multiple “error” events
  • missing “error” events sends everything to hell
  • often unsure what requires “error” handlers
  • “error” handlers are very verbose
  • callbacks suck

当作者写道“你可能根本得不到回调(迷失在 hell 中)”时,指的是什么具体问题?

最佳答案

这意味着 error 陷入了困境,因为操作函数没有“获得回调”,也就是说,错误被“吞没”了,因为没有回调来处理它。

var foo = function(onSuccess, onFailure) {
// ...
// uh-oh, I failed
if(onFailure) {
onFailure(err);
}
else {
// well, that probably wasn't too important anyway...
}
}

foo(function() { console.log("success!"); } /* no second argument... */);

请注意,在同步编码(例如,大多数 Java)中,这种情况很难发生。 Catch block 得到了更好的执行,如果异常逃脱,它会转到未捕获的异常处理程序,默认情况下会使系统崩溃。在 node 中也是如此,except 在上面的范例中没有抛出异常,它很可能被吞噬了。

在我上面的简单示例中,强大的社区惯例可以解决它,但一般来说,惯例不能完全解决这个问题。参见例如Q支持 done 方法的 promise 库。

Q.fcall(promisedStep1)
.then(promisedStep2)
.then(promisedStep3)
.then(promisedStep4)
.then(function (value4) {
// Do something with value4
})
.catch(function (error) {
// Handle any error from all above steps
})
.done();

那里的 done 调用指示 promise 链抛出任何未处理的异常(如果 catch block 丢失,或者 catch block 本身抛出)。但是调用 done 完全是程序员的责任,因为它必须调用,因为只有程序员知道链何时完成。如果程序员忘记调用 done,错误将悬在 promise 链中。我遇到过由此引起的真正的生产错误;我同意,这是一个严重的问题。

老实说,帖子中的很多内容对我来说意义不大。但我是一名经验丰富的 Node.js 程序员,这是我唯一能想到的意思。

关于javascript - TJ 的告别帖子中 Node.js 错误处理中的 "not get a callback at all"是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24602330/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com