Gregion Gregion - 1 year ago 64
Node.js Question

node.js passing value to a variable in async mode

My problem is the following:

I have a nice folder crawler function, which grabs the paths' of the files. I'm (I would like to) use these files for testing purposes.

1.) Grabbing the files
2.) Do some testing
3.) Job Done

This is the code where I call it:

walk(csvRoot, function(err, results){
if (err) throw err;
for (var i = 0; i < results.length; i++) {
return results[i] // - not working

My main issue is, that I really would like to pass the results to a variable, which will contain these paths as an array, but so far no luck.

The variable returns as undefined, that's what I'm trying to resolve currently.

Could you please advise how to do so?

Answer Source

Why do you use return within the for loop? What do you expect to return there? In any case, if you are expecting to have the results available outside of the scope of the walk function, it will not work. I pressume that you need something like that:

function getFiles (csvRoot, callback) {
  walk(csvRoot, function (err, results) {
    if (err) {
      return callback(err);

    return callback(null, results);

getFiles(csvRoot, functions (err, files) {
  // @todo: check for error

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download