为何Node.js的回调函数第一个参数是err
PHP中文网
PHP中文网 2017-04-17 13:15:56
0
4
585
fs.readFile('/etc/passwd', function (err, data) {
  if (err) throw err;
  console.log(data);
});

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

PHP中文网
PHP中文网

认证高级PHP讲师

reply all(4)
大家讲道理

It’s all because ES6 came late

小葫芦

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

Ty80

Personally, if the api is designed

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

It will be more elegant.

迷茫

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template