fs.readFile('/etc/passwd', function (err, data) {
if (err) throw err;
console.log(data);
});
在node.js中为何回调函数的第一个参数是err(错误)?
高洛峰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
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.
迷茫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