Your
readFiles
function does not return a
Promise
. The
printFiles
function has nothing to
await
.
If you're using Node.js v10 or later, you can use
fsPromises.readdir[
^] instead:
import { readdir } from 'node:fs/promises';
async function readFiles() {
const testFolder = "./files_to_move/";
return await readdir(testFolder);
}
async function printFiles() {
const filesToMove = await readFiles();
console.log(filesToMove.length);
filesToMove.forEach(item => console.log(item));
}
Otherwise, you'd need to convert the call to a promise and return that:
function readFiles() {
const testFolder = "./files_to_move/";
return new Promise((resolve, reject) => {
fs.readdir(testFolder, (err, files) => {
if (err) {
reject(err);
} else {
resolve(files);
}
});
});
}
async function printFiles() {
const filesToMove = await readFiles();
console.log(filesToMove.length);
filesToMove.forEach(item => console.log(item));
}