0

THIS IS PSUEDO CODE:

var fs = require('fs');   
var _ = require('lodash'); 
var fileNames = []
fs.readdir(dirPath, function(err,list){
    _.forEach(list, function(f){
        fileNames.push(f);
     });
}); 
//Do something with fileNames here

At the point of trying to do something with fileNames it yields an empty collections...how do I set up this code so I can use the string array compiled from interrogating a particular directory

Ryan Fisch
  • 2,614
  • 5
  • 36
  • 57
  • 2
    Does "pseudo code" just mean "code you were too lazy to make work"? – djechlin Nov 18 '15 at 17:31
  • @dhechlin don't be a dick, I was looking for the spirit of the problem not just copy and paste solution. I was just using code to try and illustrate my point. – Ryan Fisch Nov 18 '15 at 17:46

3 Answers3

2

The problem is that you are expecting the asynchronous call to populate the variable immediately, which isn't the case. The asynchronous call returns the result in the callback; the code following the asynchronous call is executed before the callback. You have two options to get what you want.

Your first option is to use the asynchronous method as you have, which is recommended, but put your "do something with fileNames here" code in the right place:

var fs = require('fs');
fs.readdir(dirPath, function(err, fileNames) {
    if (err) { /* handle error */ }
    //Do something with fileNames here
});

Your second option is to use the synchronous method, which already returns what you want:

var fs = require('fs');   
var fileNames = fs.readdirSync(dirPath);
//Do something with fileNames here

Note that in general with Node, the async (first) method is preferred, however if this was in application startup code or a command-line script, you'd probably get away with the sync method.

Also note that neither of these uses underscore. The value (list in the callback, or return value from sync method) is the list that you want, you don't need to populate into another variable.

leftclickben
  • 4,564
  • 23
  • 24
0
var fs = require('fs');   
var _ = require('lodash'); 
var fileNames = []
fs.readdir(dirPath, function(err,list){
    _.forEach(list, function(f){
        fileNames.push(f);
     });
    //Do something with fileNames here
}); 

You can do somethign file fileNames whereever you want and your program should still run. The issue is that fileNames is going to be an empty array unless you do your operation only after the callback to fs.readdir completes

dm03514
  • 54,664
  • 18
  • 108
  • 145
  • not only you just copy-pasted the op code, you didn't even check the error that may be passed... – David Haim Nov 18 '15 at 17:35
  • He didn't, he moved the "Do something..." part – leftclickben Nov 18 '15 at 17:36
  • Totally get putting all the async code in readdir callback, but how do I return these values from the module that all this code is contained in? The end goal of all this is that I am creating a json object outside of that code which is the return of a module? – Ryan Fisch Nov 18 '15 at 17:45
  • Thanks for your answers, it was the sync implementation of readdir that I was unaware and I agree the async would be preferred, but i this case I'm loading endpoints that need to be cached prior to any code being executed so I think this approach will work well. – Ryan Fisch Nov 18 '15 at 17:50
0

What you are doing is absolutely correct except that you are Doing job with files in the wrong place and one more thing your dirPath = undefined

var fs = require('fs');   
var _ = require('lodash'); 
var fileNames = [];
dirPath = './';
fs.readdir(dirPath, function(err,list){
    console.log(list);
    _.forEach(list, function(f){
        fileNames.push(f);
     });
    //Do something with fileNames here
    console.log(fileNames);
});