Ranga RS
Ranga RS

Reputation: 142

Node.js ReadFile() function syntax explanation

Could someone please explain me the node.js ReadFile() function syntax? I don't understand why function(err,data) falls inside it. I'm totally new to programming. The example on node.js website is still confusing. thanks!

sample code from node.js website

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

Upvotes: 1

Views: 838

Answers (2)

Bhushan K
Bhushan K

Reputation: 29

Study about Asynchronous Callbacks in 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

Upvotes: 1

ChaosPattern
ChaosPattern

Reputation: 124

ReadFile method reads the file that is define in the first parameter. 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.

Upvotes: 1

Related Questions