1

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?

1
  • Did you walk throw async module to avoid this issue or callback? Commented Oct 8, 2016 at 12:36

1 Answer 1

1

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

  console.log(files);
});
Sign up to request clarification or add additional context in comments.

4 Comments

It makes so much more sense now! Thank you for the quick and precise response!
@Gregion If that was helpful, you can accept the answer :)
I still have to wait 4 more minutes, to do so :)
@Gregion You can try to shrink even more the get files function if you don't want to process the results at all into the walk callback function. It can be like that: function getFiles (csvRoot, callback) { walk(csvRoot, callback); }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.