This is my first day in NodeJS and I need to write a small a program that checks for a certain word in a list of files and folders and prints out an array of files that contains this word,
I have written the program but unfortunately i wrote it in a synchronous way, not knowing that NodeJS is asynchronous, after searching the internet I found out the problem but I can't still figure out the solution.
Here is the code that I wrote:
// Get needed arguments for running the script
// USAGE: node search [EXT] [TEXT]
var args = process.argv.slice(2);
if(args[0].length == 0 && args[1].length == 0) { 
    console.log("USAGE: node search [EXT] [TEXT]");
    process.exit();
}
ext_args = args[0];
word_args = args[1];
var fs = require('fs'); // include file reader module
var path = process.cwd(); // get current path of script
var fileList = getFiles(path, ext_args, word_args);
console.log(fileList);
console.log("end");
/*
Function that get files recursively in a current script path
*/
function getFiles(path, ext, word) {
    var fileList = [];
    fs.readdir(path, function(err, items) {
        for (var i=0; i<items.length; i++) {
            var currentFile = items[i];
            var itemPath = path + "\\" + currentFile;
            if(fs.lstatSync(itemPath).isDirectory()) {              
                return getFiles(path + '\\' + currentFile, ext, word);
            } else {
                if(currentFile.endsWith(ext)) {
                   fileList.push(checkString(itemPath, word));
                } else {
                }
            }
        }
    });
    return fileList;
}
/*
 Function that checks if the word exist in the text file
*/
function checkString(filePath, word) {
    fs.readFile(filePath, 'utf8', function(err, data) {
        //console.log("> Checking String");
        if(err) throw err;
        if(data.indexOf(word) >= 0) {           
            return filePath;
        } else {          
        }
    })
}
When I print fileList I get it as an empty array. Can you show me how to write the recursive function in a proper way?
 
     
    