fsPromises.opendir(path[, options])


异步地打开目录进行迭代扫描。 有关更多详细信息,请参阅 POSIX opendir(3) 文档。

创建 <fs.Dir>,其中包含用于从目录读取和清理目录的所有进一步的函数。

encoding 选项设置在打开目录和随后的读取操作时 path 的编码。

使用异步迭代的示例:

import { opendir } from 'fs/promises';

try {
  const dir = await opendir('./');
  for await (const dirent of dir)
    console.log(dirent.name);
} catch (err) {
  console.error(err);
}

当使用异步迭代器时,<fs.Dir> 对象将在迭代器退出后自动关闭。

Asynchronously open a directory for iterative scanning. See the POSIX opendir(3) documentation for more detail.

Creates an <fs.Dir>, which contains all further functions for reading from and cleaning up the directory.

The encoding option sets the encoding for the path while opening the directory and subsequent read operations.

Example using async iteration:

import { opendir } from 'fs/promises';

try {
  const dir = await opendir('./');
  for await (const dirent of dir)
    console.log(dirent.name);
} catch (err) {
  console.error(err);
}

When using the async iterator, the <fs.Dir> object will be automatically closed after the iterator exits.