2021 年更新:
Nodejs 中已修复了歧义doc in an 2021 年 4 月承诺
编辑:这本质上是节点文档的问题。查看讨论here.
我正在尝试fs.Dir自 Node v12.12 以来添加的类:
// 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)
。我究竟做错了什么?
dir 对象的异步迭代器将在迭代完成时自动关闭 dir。这似乎没有记录,但您可以在代码中看到here.
async* entries() {
try {
while (true) {
const result = await this[kDirReadPromisified]();
if (result === null) {
break;
}
yield result;
}
} finally {
await this[kDirClosePromisified]();
}
}
}
ObjectDefineProperty(Dir.prototype, SymbolAsyncIterator, {
value: Dir.prototype.entries,
enumerable: false,
writable: true,
configurable: true,
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)