等待JS中没有分隔符的迭代函数 [英] Await an iterative function without delimiter in JS
问题描述
我有一个目录,其中包含未知数量的子文件夹.每个子文件夹可能具有或没有其他子文件夹.我正在使用递归函数来遍历它们.由于子文件夹数量不明,我缺少一种在继续之前确保所有文件夹都已被检查的方法.我对异步和等待的了解非常有限.有什么办法可以解决这个问题?
I've got a directory with an unknown amount of subfolders. Each subfolder might have or not further subfolders. I am itterating through them using a recursive function. Due to the unknown amounts of subfolders I am missing a way to make sure that all folders have been checked before I continue. My knowledge on async and await is quiet limited. Is there any Way to handle this problem?
function searchForPackage(directory){
fs.readdir(directory, function(err, files){
if(err){
return;
}else{
files.forEach(file => {
var currentLocation = directory + "/" + file;
if(fs.statSync(currentLocation).isDirectory() && file != 'bin' && file != '.bin'){
searchForPackage(currentLocation);
return;
}else if(file == "package.json"){
var content = fs.readFileSync(currentLocation);
var jsonContent = JSON.parse(content);
var obj = {
name: jsonContent.name,
license: jsonContent.license,
version: jsonContent.version
}
jsonTable.push(obj);
jsonTable.push({name: jsonContent.name, license: jsonContent.license, version: jsonContent.version});
return;
}
})
}
})
}
推荐答案
您有一些选择:
1)由于其他所有操作都是使用fs
的同步方法完成的,因此可以将fs.readdir
更改为fs.readdirSync
:
1) Since everything else is done using fs
's synchronous methods, you could change fs.readdir
to fs.readdirSync
:
function searchForPackage(directory) {
fs.readdirSync(directory).forEach(file => {
var currentLocation = directory + "/" + file;
if (fs.statSync(currentLocation).isDirectory() && file != 'bin' && file != '.bin') {
searchForPackage(currentLocation);
return;
} else if (file == "package.json") {
var content = fs.readFileSync(currentLocation);
var jsonContent = JSON.parse(content);
var obj = {
name: jsonContent.name,
license: jsonContent.license,
version: jsonContent.version
}
jsonTable.push(obj);
jsonTable.push({name: jsonContent.name, license: jsonContent.license, version: jsonContent.version});
return;
}
})
}
2)将fs.readdirSync
转换为Promise
,然后使用async/await
:
2) Convert fs.readdirSync
to a Promise
and then use async/await
:
async function searchForPackage(directory) {
const files = await new Promise((resolve, reject) => {
fs.readdir(directory, (err, files) => {
if (err) reject(err);
else resolve(files);
});
});
await Promise.all(files.map(async file => {
var currentLocation = directory + "/" + file;
if (fs.statSync(currentLocation).isDirectory() && file != 'bin' && file != '.bin') {
await searchForPackage(currentLocation);
return;
} else if (file == "package.json") {
var content = fs.readFileSync(currentLocation);
var jsonContent = JSON.parse(content);
var obj = {
name: jsonContent.name,
license: jsonContent.license,
version: jsonContent.version
}
jsonTable.push(obj);
jsonTable.push({name: jsonContent.name, license: jsonContent.license, version: jsonContent.version});
return;
}
}))
}
3)使用几个第三方模块来进行一些清理( fs-extra
会为您处理多样化的异步方法,例如fs.readdir
. async-af
提供可链接的异步JavaScript方法,例如并行的 forEach
.):
3) Use a couple third-party modules to clean things up a bit (fs-extra
takes care of promisifying asynchronous methods like fs.readdir
for you. async-af
provides chainable asynchronous JavaScript methods such as a parallel forEach
.):
const fs = require('fs-extra');
const AsyncAF = require('async-af');
async function searchForPackage(directory) {
await AsyncAF(fs.readdir(directory)).forEach(async file => {
var currentLocation = directory + "/" + file;
if (fs.statSync(currentLocation).isDirectory() && file != 'bin' && file != '.bin') {
await searchForPackage(currentLocation);
} else if (file == "package.json") {
var content = fs.readFileSync(currentLocation);
var jsonContent = JSON.parse(content);
var obj = {
name: jsonContent.name,
license: jsonContent.license,
version: jsonContent.version
}
jsonTable.push(obj);
jsonTable.push({name: jsonContent.name, license: jsonContent.license, version: jsonContent.version});
}
});
}
这篇关于等待JS中没有分隔符的迭代函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!