fs.readFile('/etc/passwd', function (err, data) { if (err) throw err; console.log(data); });
在node.js中为何回调函数的第一个参数是err(错误)?
认证高级PHP讲师
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
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
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
Personally, if the api is designed
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