If you don't want help and just want a straight answer as to why, the
answer is "because". If you don't like the syntax use promises or some
compile to JS language to clean it up. But `.bind()` is the way to do
this, and it isn't ugly it is just JavaScript.

Don't use JavaScript if you don't like JavaScript. I don't think running
millions of file IO operations is good design in the first place.

-Chad

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf
Of Michael Hasenstein
Sent: Wednesday, December 12, 2012 6:53 AM
To: [email protected]
Subject: Re: [nodejs] Re: Why no additional parameters in (async.
node.js API) callback functions?

I know how I can do it (at least 3 different very ways came to my mind
immediately), I said so ;-) - that was't my question (or point).

I don't WANT to have to write that, if I can help it.

I would except the explanation that since node.js is very low-level
burdening the API functions with an additional 'if' to make it call the
callback with *optional* (that's what leads to the 'if') parameters
given to it for that purpose, and since the API functions are possibly
called millions of times per second, the decision was made to burden the
code using node.js instead of node.js.




On Wed, Dec 12, 2012 at 12:47 PM, Jonathan Dickinson
<[email protected]> wrote:
> You can do it with .bind(). I assume that the 1...n arguments of 
> .bind() are often overlooked.
>
> var fs = require('fs');
>
> function onStat(file, err, stat) {
>     if (stat.isFile()) {
>         //fs.readFile(...)... WHICH FILE????
>         console.log(file); // This file
>     }
> }
>
> var files = [
> "E:\\site.css",
> "E:\\stacks.txt",
> "E:\\Node",
> ];
>
> files.forEach(function (file) {
>     fs.stat(file, onStat.bind(this, file)); });
>
> On Wednesday, 12 December 2012 11:36:29 UTC+2, Michael Hasenstein
wrote:
>>
>> This is not a technical question (I'm quite clear about how the stuff

>> works). I also did some (Google) research before asking.
>>
>> I'm just curious if there is a good reason that I just fail to see...

>> I AM aware that very obviously I am not the first person to think 
>> about this, but I just could not find ANY good explanation for the
"WHY".
>>
>> Let me just give an example.
>>
>> I get an array of strings (filenames, e.g. from fs.readDir), and now 
>> I want to process them: fs.stat(), fs.readFile(), then minify, then 
>> fs.writeFile().
>>
>> now. all those operations are asynchronous unless I use the 
>> sync-version of those functions.
>>
>> PROBLEM:
>>
>> I really, really, REALLY need that filename string it all started 
>> with in the other functions - so now, with node.js callback API being

>> as it is, I have to write code that I really, REALLY dislike, because

>> it seems suboptimal compared to what I COULD do.
>>
>> What I COULD do but which the node.js callbacks don't allow is the 
>> passing of additional parameters to my callback.
>>
>> Code:
>> function onStat(err, stat) {
>>     if (stat.isFile()) {
>>         //fs.readFile(...)... WHICH FILE????
>>     }
>> }
>>
>> files.forEach(function (file) {
>>     fs.stat(<some path> + file), onStat); });
>>
>> I AM AWARE HOW TO SOLVE THIS. Pls. don't reply showing me how I can 
>> easily solve this with additional function scopes.
>>
>> My issue with adding additional functions is that that solution 
>> SUCKS. If I could just add additional parameters to the fs.stat() 
>> call which my callback gets as 3rd, 4th, etc parameter (or an array 
>> or an object,
>> whatever) the sun would still shine.
>>
>> However, node.js makes me add additional quite useless scopes.
>> ALTERNATIVELY I write all those callback functions into the lexical 
>> scope of the forEach() - that's what has been called "callback hell" 
>> for a long time
>> - no way.
>>
>> So, can anyone enlighten me - and I MAY INDEED be simply incredibly 
>> stupid not to see the point without help - why node.js could not just

>> let me add custom parameters for callbacks? Again: additional 
>> scope-producing functions are NOT OPTIMAL IMHO - it produces overhead

>> both in the code and during runtime. There MUST be a reason, 
>> otherwise by now, node.js almost at version 0.9, would have been 
>> changed, wouldn't it? I mean, libraries like YUI3 give me the option 
>> to add my own custom parameters to be passed down to callback
functions, to solve this exact problem.
>>
>> TIA!
>>
> --
> 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 [email protected] To 
> unsubscribe from this group, send email to
> [email protected]
> 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 [email protected] To
unsubscribe from this group, send email to
[email protected]
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 [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Reply via email to