search

Home  >  Q&A  >  body text

为何Node.js的回调函数第一个参数是err

fs.readFile('/etc/passwd', function (err, data) {
  if (err) throw err;
  console.log(data);
});

在node.js中为何回调函数的第一个参数是err(错误)?

PHP中文网PHP中文网2852 days ago593

reply all(4)I'll reply

  • 大家讲道理

    大家讲道理2017-04-17 13:17:56

    It’s all because ES6 came late

    reply
    0
  • 高洛峰

    高洛峰2017-04-17 13:17:56

    This is a convention~~~
    When using callbacks,
    first check whether the error exists, and if it exists, process the error message;
    Otherwise, follow the normal business logic

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 13:17:56

    Personally, if the api is designed

    fs.readFile('/etc/passwd').success(function(data){
        //todo
    }).error(function(err){
        //todo
    });

    It will be more elegant.

    reply
    0
  • 迷茫

    迷茫2017-04-17 13:17:56

    Required parameters are listed in the front, and optional parameters are listed in the back. All APIs passed are designed according to this principle

    reply
    0
  • Cancelreply