At least with my .bind() example one could argue that it makes the code
harder to read (yes, I completely agree with you about closure hell; and
they are way less understandable than what I am about to say): it has to do
with expression of intent. To the untrained eye, what is going on here?
fs.stat(file, onStat, file); // What the...
With the bind the intent is clear and we don't get confused by two params
that are exactly the same (or wonder what that extra param does in the
event they are not):
fs.stat(file, onStat.bind(this, file));
It's similar to the current religious boolean debate, one side argues that
you shouldn't use booleans:
new Cow("Nelly", true); // 'true'? Does that mean this cow is able to bark?
vs.
new Cow("Nelly", cowType.male);
The counter-argument for using named booleans is that we have
intellisense/advanced IDEs and the intent of the boolean is contextually
accessible, but that isn't *really *applicable to JS yet. Thus in an
'advanced IDE' we could hover over stat and see that the overload in use
accepted a pass-through param in that position and do away with this debate.
Had the core nodejs libraries used this style from day 1 it would have been
expected style (and therefore not surprising), but they didn't: so now we
have a bunch of 3rd party modules that [correctly] followed that original
style to remain consistent and changing things would only lead to confusing
code.
Remember you could opt-into the style you want by writing a generic module
wrapper, similar to what promise-io does (where you can ask it to wrap a
node function for you). And there-in lies the power in the current style:
*YOU* can make the choice. Even though the community went for the closure
route, if you disagree you are one small wrapper module away from 'fixing
it'.
On Wednesday, 12 December 2012 13:53:11 UTC+2, Michael Hasenstein wrote:
>
> 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] <javascript:>> 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]<javascript:>
> > To unsubscribe from this group, send email to
> > [email protected] <javascript:>
> > 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