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