【发布时间】:2021-01-13 23:21:24
【问题描述】:
我正在编写一个递归函数,它创建一个选定文件目录的对象树。我的代码有效,但顺序错误。我看不到我的代码的输出。代码如下:
const fs = require("fs");
const basePath = process.argv[2];
const result = {};
const isDirectory = path => {
return new Promise((resolve, reject) => {
fs.lstat(path, (err, stats) => {
if (err) reject("No such file or Directory");
resolve(stats.isDirectory());
});
});
};
const createTree = (path, target) => {
return new Promise((reject, resolve) => {
fs.readdir(path, (err, list) => {
for (const item of list) {
const currentLocation = `${path}/${item}`;
isDirectory(currentLocation).then(isDir => {
console.log(result); //I CAN SEE THE RESULT HERE
if (!isDir) {
target[item] = true;
} else {
target[item] = {};
resolve(createTree(currentLocation, target[item]));
}
});
}
});
reject("Somthing went wrong while getting the list of files");
});
};
createTree(basePath, result)
.then(() => console.log("result --->", result)) //BUT NOT HERE
.catch(err => console.log("Consume Error ==>", err));
我也使用 async await 完成了它,但我很好奇为什么它不适用于 Promise。
这是async await 的完整工作示例:
const fs = require("fs");
const basePath = process.argv[2]; //Getting the path
const result = {};
//Function to check my path is exist and it's a directory
const isDirectory = async path => {
try {
const stats = await fs.promises.lstat(path); //Used istat to get access to the "isDirectory()" method
return stats.isDirectory();
} catch (error) {
throw new Error("No such file or Directory");
}
};
//Recursive function that should create the object tree of the file system
const createTree = async (path, target) => {
try {
const list = await fs.promises.readdir(path);
for (const item of list) {
const currentLocation = `${path}/${item}`;
const isDir = await isDirectory(currentLocation);
//If it's a file, then assign it to true
//Otherwise create object of that directory and do the same for it
if (!isDir) {
target[item] = true;
} else {
target[item] = {};
await createTree(currentLocation, target[item]);
}
}
} catch (err) {
console.log("Somthing went wrong while getting the list of files");
}
};
//Consuming the createTree function
(async () => {
try {
await createTree(basePath, result);
console.log(result);
} catch (error) {
console.log(error.message);
}
})();
【问题讨论】:
-
我没有看到你在第一个版本中调用 resolve,在 createTree 中。您需要在某个地方解决承诺以使其发挥作用。使用 async/await 当然“它是自动完成的”
-
在
createTree,new Promise((reject, resolve) => {-->new Promise((resolve, reject) => {,另外你也不要打电话给resolve。 -
你真的应该看看
const fs = require('fs/promises'); -
@sp00m 我尝试调用resolve并将递归调用放入其中,它返回的结果与没有resolve的情况相同,在我尝试过的其他地方它只是拒绝。你能帮我吗?感谢您的关注))
-
你不能多次
resolve一个Promise。这就是您for循环正在尝试做的事情。您复制previous question 有什么原因吗?那里提供的答案有什么问题?
标签: javascript node.js recursion promise