To further explain this.  This is what node calls an async function.  It's
supposed to be the analogue to a sync function, except that it doesn't
block the program (the call to fs.readFile will return right away).

If this was a sync function (and in fact, there is a sync version of this
one), it would look like:

var data
try {
  data = fs.readFileSync("file1.txt", "utf8")
} catch (err) {
  // Handle error
}

Where data is the same thing you're getting in the callback in the
non-blocking version.  If there is an error, it's thrown as an exception.

Async functions in node always accept the callback as the last arg and
reserve the first argument in the callback for any potential errors that
might come up.  (Because try..catch doesn't work in non-blocking functions
since they exit the try block before they call the callback)


On Mon, Nov 26, 2012 at 10:57 AM, Dave Johnson
<davejohnson10...@gmail.com>wrote:

> Hi Charles,
>
> That is kind of what I was thinknig was going on perfectly clarified by
> yourself. Good to ask and get a full explanation to these parameters and
> like you say in the event of an error which parameters are null.
>
> On Monday, 26 November 2012 16:17:14 UTC, Charles Care wrote:
>
>> Hi Dave,
>>
>> The callback-based APIs follow a convention where the first positional
>> parameter is for error information. You're asking the Node 'fs' library to
>> read 'file1.txt'. If the read is successful, then the 2nd param ('data')
>> will contain a representation of the file's contents, and the 1st param
>> will be null. Alternatively, if an error occurs, then the err parameter
>> will have a value representing the error, and the 2nd param will be either
>> null or undefined.
>>
>> The parameter names are a convention: you could call them 'apple' and
>> 'orange', but that would really make your code hard to maintain!
>>
>> Hope that helps,
>>
>> Charles
>>
>> On 26 November 2012 15:50, Dave Johnson <davejohn...@gmail.com> wrote:
>>
>>> Hello and I apologise if this is a stupid question but I am reading up
>>> on Callbacks in node.js and wanted to ask something about it.
>>>
>>> Example code:
>>>
>>> fs.readFile('file1.txt', 'utf8', function (err, data) {
>>>       if (err) throw err;
>>>       console.log('File 1 read!');
>>>   });
>>>
>>> I just dont know what the parameters *err* & *data* are, is this
>>> something that the node.js engine resolves internally? Hope I am making
>>> sense, in other coding you would create these variables and then pass them
>>> to a method.
>>>
>>> Thanks,
>>>
>>> Dave Johnson
>>>
>>>
>>  --
> Job Board: http://jobs.nodejs.org/
> Posting guidelines:
> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to nodejs@googlegroups.com
> To unsubscribe from this group, send email to
> nodejs+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en
>

-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nodejs@googlegroups.com
To unsubscribe from this group, send email to
nodejs+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Reply via email to