【发布时间】:2020-12-29 13:19:09
【问题描述】:
2021 年更新: 歧义已在 nodejs doc 中的 April 2021 commit
中得到修复编辑:这本质上是节点文档的问题。见讨论here。
我正在尝试自节点 v12.12 以来添加的 fs.Dir 类:
// hello-world.js
const fs = require('fs');
(async function () {
const dir = await fs.promises.opendir(__dirname);
for await (const dirent of dir) {
console.log('name:', dirent.name, 'isDir:', dirent.isDirectory());
}
return dir.close();
})();
$ node hello-world.js
它似乎按预期工作,它会注销目录中每个文件的信息(没有丢失一个),但最后它会抛出(node:3218) UnhandledPromiseRejectionWarning: Error [ERR_DIR_CLOSED]: Directory handle was closed at Dir.close (internal/fs/dir.js:161:13)。我做错了什么?
【问题讨论】:
标签: node.js