I have a function that calls the readFiles function, and parses the data into data{}, but once outside the loop, data == {}, and that is not what I am trying to achieve. Any pointers of where I should be looking?
data = {}; readFiles({ dirname: path.join(__dirname, '../ping/'), onFileContent: (filename, content) => { data[filename] = content; console.log(data); }, onError: function (error) { console.log(error); } }); console.log(data); As I stated, the console.log(data) at the end has {} as it's value.
Here is readFile
function readFiles(parameters) { var dirname = parameters.dirname; var onFileContent = parameters.onFileContent; var onError = parameters.onError; fs.readdir(dirname, (err, filenames)=>{ if (err) { onError(err); return; } filenames.forEach(filename => { return fs.readFile(path.resolve(dirname, filename), 'utf-8', (err, content)=>{ if (err) { onError(err); return; } onFileContent(filename, content); }); }); }); } If I change the second block to use readFileSync, I get errors that I have no idea how to correct.
readFilesdo? Is it asynchronous? It looks like it.