why? its the simpliest and most common way, that's it. passing by custom 
params has to be implemented in the async function itself, there is no 
native support in js nor node for this. and since use of clojures is very 
common in js world, mostly noone cares about it. but yes it may help to 
un-clutter the callbacks a bit and helpt to build pure functions with 
explicit parameters. but i doubt that it would be a performance 
improvement, because v8 optimized clojurescope-access very well, see 
here http://jsperf.com/bind-vs-clojure/2

Am Mittwoch, 12. Dezember 2012 10:36:29 UTC+1 schrieb Michael Hasenstein:
>
> 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 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