【发布时间】:2022-01-08 15:17:31
【问题描述】:
我想获得一些帮助,因为我不知道如何在 Node.js 中递归地使用 async-await 方法。
我正在尝试创建一个函数,该函数使用文件系统模块将所有子文件夹中的所有文件作为数组返回。
我确实在网上看到了很多示例,但没有一个使用数组,而是等待答案。
谢谢!
function checkFiles () {
const files = []
const getFiles = async dir => fs.readdir(`./${dir}`, { withFileTypes: true }, (err, inners) => {
if (err) {
throw new Error (err)
}
else {
inners.forEach(inner => {
inner.isDirectory() ? getFiles(`${dir}/${inner.name}`) : files.push(`file: ${inner.name}`);
});
};
});
getFiles('.')
if (files.length === 0) {
return 'no files'
}
else {
return files
}
}
console.log(checkFiles())
【问题讨论】:
标签: javascript node.js asynchronous async-await promise