0
if (args[0] === "help") {
    var help_array = [];
    fs.readdir('./commands/', (err, files) => {
        files.forEach((response) => {
            var commandFile = require('./commands/' + response);
            help_array.push(commandFile.getHelp());
            console.log(help_array); // <----- array with elements
        });
    });
    console.log(help_array); // <----------- empty array []
    return;
}

Why is this happening? How i can solve this? It is related to async function?

zxvnme
  • 41
  • 1
  • 7
  • 3
    The `.readdir()` function is **asynchronous**. – Pointy Jan 16 '18 at 18:33
  • you can use `fs.readDirSync` for more reference https://nodejs.org/api/fs.html#fs_fs_readdirsync_path_options and to have better understanding about synchronous and asynchronous https://www.pluralsight.com/guides/front-end-javascript/introduction-to-asynchronous-javascript – John Michael Villegas Jan 17 '18 at 02:07

0 Answers0