var path;
for (var i = 0, c = paths.length; i < c; i++)
{
path = paths[i];
fs.lstat(path, function (error, stat)
{
console.log(path); // this outputs always the last element
});
}
How can I access the path
variable, that was passed to fs.lstat function?
Recursion works nicely here (especially if you have some i/o that must be executed in a synchronous manner):
This is a perfect reason to use
.forEach()
instead of a for loop to iterate values.Also, you could use a closure like @Aadit suggests:
Classic problem. Put the contents of the for loop in another function and call it in the loop. Pass the path as a parameter.