在nodejs的每个callback之后,我是否需要检查错误?

我正在按照节点JS第一个error handling约定。 callback是以callback(null, response) 。 收到callback响应后,是否每次都检查是否为空?

例:

 methodCB(function (error, response) { if (error) { //handle error //Do i have to check this every single time for a callback? //Are there design alternatives for this or is this just a node thing? } else { //process response } }); 

像往常一样,错误可能是操作错误或程序员错误。

处理和抛出这些错误之间的select是一个相当深入的话题。 Node.jserror handling的规范指南是Node.js最佳实践exception处理 ,我强烈build议您阅读。 本文重点介绍了您应该特别处理错误的情况,以及其他您不应该(而是确保您的应用程序正常失败)的情况。

不pipe错误的来源和处理/抛出的select,正如jfriend00所build议的,所有健壮的代码必须注意任何可能的错误返回。