I have a function that reads files in a directory and pushes their name into an array. But every time I try to print the array, it's empty because the code does not wait for the function to finish, even though I used await that suppose to make the code wait for the function.
here is the code:
let filesToMove = [];
async function readFiles() {
const testFolder = "./files_to_move/";
fs.readdir(testFolder, (err, files) => {
files.forEach((file) => {
filesToMove.push(file);
console.log("done");
});
});
}
async function printFiles() {
files = await readFiles();
console.log(filesToMove.length);
filesToMove.forEach((item) => {
console.log(item);
});
}
Thank you in advance