Skip to content Skip to sidebar Skip to footer

Add Additional Parameters To Callback Function

I'm building a system in Node.js that is supposed to find all files in an array of folders, stat them, and then do some additional work using that information. I'm using fs.readdir

Solution 1:

There is no block scope, so use a function for scope.

for(var i=0, path, max=paths.length; i<max; i++) {
    path = paths.pop();
    console.log("READING PATH: " + path);
    handlePath( path );
}
functionhandlePath( path ) {
    fs.readdir(path, onPathRead);
    functiononPathRead(err, files) {
        handleDir(err, files, path);
    }
}

Solution 2:

This is one of the more annoying parts of JS for me. An alternative to creating a separate function (as @generalhenry demonstrated) is to wrap the code in an anonymous function that's executed before the path variable changes.

for(i=0,max=paths.length; i<max; i++) {
    var path = paths.pop();
    console.log("READING PATH: " + path);
    fs.readdir(path,
        (function(p){
            returnfunction(err, files) {
                handleDir(err, files, p);
            };
        })(path);
    );
}

Either way, the important point is that the function's context (anonymous or not) is initiated before the value of the path variable is reassigned.

Solution 3:

This is indeed an annoying feature of Javascript, so much so that Coffeescript (which is a language that compiles down to Javascript) has a specific way of dealing with it, the do operator on for. In Coffeescript your original function would be:

forpathin paths
     fs.readdir path, (err, files) -> handleDir(err, files, path)

and the solution to your problem would be:

forpathin paths
   do (path) ->
     fs.readdir path, (err, files) -> handleDir(err, files, path)

Solution 4:

I was looking for the same thing and end up with the solution and here it's a simple example if anybody wants to go through this.

varFA = function(data){
   console.log("IN A:"+data)
   FC(data,"LastName");
};
varFC = function(data,d2){
   console.log("IN C:"+data,d2)
};
varFB = function(data){
   console.log("IN B:"+data);
    FA(data)
};
FB('FirstName')

Solution 5:

Great solution from generalhenry, but be careful if you're going to use a try/catch structure inside the callback function

functionhandlePath( path ) {
    fs.readdir(path, onPathRead);
    functiononPathRead(err, files) {
        try {
            handleDir(err, files, path);
        } catch (error) {
            var path = 'something_else'; // <<--- Never do this !!!
        }     
    }
}

Never try to redeclare the same var in a catch block, even if the catch block is never invoked, the path var is reset and you will find it as 'undefined' when the callback is executed. Try this simple example:

functionwrapper(id) {
    console.log('wrapper id:' + id);
    setTimeout(callback, 1000);
    functioncallback() {
        try {
            console.log('callback id:' + id);
        } catch (error) {
            var id = 'something_else';
            console.log('err:' + error);
        }
    }
}

wrapper(42);

This will output:

wrapper id:42
callback id:undefined

Post a Comment for "Add Additional Parameters To Callback Function"