fsPromises.opendir(path[, options])
path<string> | <Buffer> | <URL>options<Object>encoding<string> | <null> 默认值:'utf8'bufferSize<number> 从目录读取时内部缓冲的目录项数量。数值越大,性能越好,但内存使用也越高。默认值:32recursive<boolean> 已解析的Dir将是一个 <AsyncIterable>,包含所有子文件和子目录。默认值:false
- 返回:<Promise> 使用 <fs.Dir> 完成。
异步打开目录以进行迭代扫描。有关更多详细信息,请参阅 POSIX opendir(3) 文档。
【Asynchronously open a directory for iterative scanning. See the POSIX
opendir(3) documentation for more detail.】
创建一个 <fs.Dir>,其中包含用于从目录中读取和清理的所有后续功能。
【Creates an <fs.Dir>, which contains all further functions for reading from and cleaning up the directory.】
encoding 选项用于设置在打开目录以及后续读取操作时 path 的编码。
【The encoding option sets the encoding for the path while opening the
directory and subsequent read operations.】
使用异步迭代的示例:
【Example using async iteration:】
import { opendir } from 'node:fs/promises';
try {
const dir = await opendir('./');
for await (const dirent of dir)
console.log(dirent.name);
} catch (err) {
console.error(err);
} 使用异步迭代器时,<fs.Dir> 对象将在迭代器退出后自动关闭。
【When using the async iterator, the <fs.Dir> object will be automatically closed after the iterator exits.】