1

I was learning Node.js (even though I am not an expert in Javascript but I understand and write code on it). Now, trying to play with Node.js, I just got stuck in this code:

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

Here is some of my confusions: The anonymous function is taking two arguments: err and data and inside the function err is for any error while reading the file which is thrown and data is the actual contents of the file.

  1. Well, how the function knows and differentiate between which one is error and which one is content of the file?
  2. Does it do like: the first argument is always an error and second argument is always the content data?
  3. what err and data will have inside the function if I write like this

    function(data, err) {} ?
    

This is just a simple function being passed just two arguments. How it works for some more arguments?

How inside the function, data is data and err is error?

For the above example, are err and data are pre-defined keywords (I doubt NO)?

0

3 Answers 3

3

The arguments will get values in the order they're passed while calling the function. You can see what's the correct order by reading the API docs for the function you plan to use.

So, if you change your function to function(data, err) {}, then data will contain the error, while err will hold the data :)

To ease your future work, nearly every Node.js function which accepts an callback, the 1st arg will be the error and the 2nd arg the return of the function.

Sign up to request clarification or add additional context in comments.

3 Comments

Ok, all my confusions are gone now, thank you so much ... So, there is always pre-defined and written the function about the precedence of the passing arguments, right? javascript is really weird at first but I love it.
JS is the most dynamic language I'm aware of, that's why it looks complicated at first to everyone.
@jeewan I've also edited the answer to add more info about callbacks in node
2

1). Well, how the function knows and differentiate between which one is error and which one is content of the file?

The order of the arguments is known when the function is invoked from Node.

2). Does it do like: the first argument is always an error and second argument is always the content data?

Yes.

3). what err and data will have inside the function if I write like this

function(data, err) {} ?

They will have the same values, they simply won't be used.

For the above example, are err and data are pre-defined keywords?

Nope. You could even rename them and they will still be bound to the same values.

Comments

1

this is how it is defined in API

check here

as pointed by @gustavohenke if you define it like

function(data, err){ ... }

then data will hold error log and err will hold file data

EDIT
hope you read this in docs it should have cleared your doubt:

The callback is passed two arguments (err, data), where data is the contents of the file.

4 Comments

I had copied the code from that link and it is this: fs.readFile(filename, [options], callback) but callback is a function passing arguments. How this recognizes between two arguments so passed?
That's a simple if inside the function definition; if the 2nd arg is a function, then options will not be used. If it's an object, then the 3rd arg must be the callback.
yeah, I saw that, much cleared and I understood it now. Thanks.
oh yes, its in their docs, I did not see it. I got it completely now :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.