简体   繁体   English

Node.js ReadFile()函数语法说明

[英]Node.js ReadFile() function syntax explanation

Could someone please explain me the node.js ReadFile() function syntax? 有人可以向我解释node.js ReadFile()函数的语法吗? I don't understand why function(err,data) falls inside it. 我不明白为什么函数(err,data)落在其中。 I'm totally new to programming. 我是编程的新手。 The example on node.js website is still confusing. node.js网站上的示例仍然令人困惑。 thanks! 谢谢!

sample code from node.js website 来自node.js网站的示例代码

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

ReadFile method reads the file that is define in the first parameter. ReadFile方法读取第一个参数中定义的文件。 When it's over then will execute the passed function or callback (second parameter). 当结束时,将执行传递的函数或回调(第二个参数)。 This function has two input parameter error and data. 该功能有两个输入参数错误和数据。 If an error is not ocurred then error will be undefined and data will contain file data. 如果未发生错误,则错误将是未定义的,并且数据将包含文件数据。

Study about Asynchronous Callbacks in JavaScript. 研究JavaScript中的异步回调。 In your question 在你的问题

fs.readfile('xyz',(err,data)=>{});

What it does is, it creates a non-blocking execution call, which means that the program does not wait for the file to be read completely. 它的作用是创建一个非阻塞执行调用,这意味着程序不会等待文件被完全读取。 The program execution ends and when the file has been read the output is logged. 程序执行结束,并且在读取文件时记录输出。 For a basic understanding of the scenario you can visit: https://www.tutorialspoint.com/nodejs/nodejs_callbacks_concept.htm 对于该场景的基本了解,您可以访问: https : //www.tutorialspoint.com/nodejs/nodejs_callbacks_concept.htm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM