fs.opendir() 创建的 fs.Dir 上的异步迭代器后 dir.close() 出错

2023-11-26

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(使用前将#替换为@)

fs.opendir() 创建的 fs.Dir 上的异步迭代器后 dir.close() 出错 的相关文章

  • 嵌套异步/等待 Nodejs

    似乎无法弄清楚为什么这对我不起作用 我有一个父函数 它对子加载进程执行 AWAIT LOAD 进程又调用另一个名为 LOADDATA 的 AWAIT 所以基本上是这样的 module exports async function try a
  • Node.js 未处理的“错误”事件

    我编写了一个简单的代码并将其保存在文件 try js 中 var http require http var makeRequest function message var options host localhost port 8080
  • Phantom-node模块无法加载外部资源

    我正在开发一个nodejs服务器 它将发布的html呈现为pdf png或jpg https github com svenhornberg pagetox https github com svenhornberg pagetox ser
  • 从节点服务器访问 Google Calendar API

    由于某种原因 我很难访问 Google 日历 我希望能够在 Node js 服务器的日历中添加和删除事件 我从文件中发现了非常矛盾的信息 我跟着 https developers google com identity protocols
  • 如何阻止 Node.js 服务器崩溃

    我是节点js新手 我试图创建一个简单的 HTTP 服务器 我按照著名的例子创建了一个 Hello World 服务器如下 var handleRequest function req res res writeHead 200 res1 e
  • web3.eth.sendSignedTransaction() 总是返回“返回错误:nonce 太低”

    I used 电子邮件受保护 cdn cgi l email protection在 Node js 中与私有区块链交互 我是按照官方文档写的代码 电子邮件受保护 cdn cgi l email protection var Web3 re
  • 如何在node.js中存根process.env?

    我想存根process env FOO with bar var sinon require sinon var stub sinon stub process env FOO bar 我很困惑 我阅读了文档 但仍然不明白 sinonjs
  • express-hbs 实例 registerAsyncHelper 奇怪的哈希值

    我正在使用express hbs nodejs模块 但在使用时遇到问题registerAsyncHelper 我需要在限制范围内编译布局 因为我创建了一个新的 Handlebars 实例 并在该实例中创建了一个助手 但是当我编译布局时 它返
  • 如何将多个 api 请求中的多个可读流传输到单个可写流?

    期望的行为 实际行为 我尝试过的 重现步骤 研究 期望的行为 将从多个 api 请求接收到的多个可读流传输到单个可写流 API 响应来自 ibm watsontextToSpeech synthesize https cloud ibm c
  • 尝试安装 LESS 时出现“请尝试以 root/管理员身份再次运行此命令”错误

    我正在尝试在我的计算机上安装 LESS 并且已经安装了节点 但是 当我输入 node install g less 时 出现以下错误 并且不知道该怎么办 FPaulMAC bin paul npm install g less npm ER
  • 如何在查询语句之外从mysql查询中获取值?

    这是下面的函数console log function quo value value connection query SELECT role from roles where id 1 function error results fi
  • 使用cheerio 检索href

    我有一个下载的 html 文件 看起来像这样 div div div div style height 11px color white font size 9px font weight bold div div style border
  • HTML 格式的 Google Apps 脚本

    是否可以在我的 HTML 中使用 google apps 脚本 我希望能够从外部框架 例如 Node js 以纯 Javascript 形式从表单写入电子表格 https developers google com apps script
  • Node.js http get 请求错误事件未拾取 404 或 403

    我正在对图像发出 HTTP GET 请求 有时图像会以 404 或 403 的形式返回 令我惊讶的是 我必须明确检查这一点 而不是在错误事件中选择它 这是它的工作原理还是我在这里遗漏了一些东西 function processRequest
  • 护照本地猫鼬帐户注册的附加字段?

    我将 Passport local mongoose 与 Node js Express js MongoDB 一起用于 Web 应用程序 我想使用用户名字段 密码字段 公司名称字段和电话号码字段 所有字段 来注册用户作为字符串 但是 我只
  • npmjs.org - 找不到自述文件

    我是 npm 包的主要作者scramjet 一个月以来 我遇到了关于可视性的问题README md在 npmjs 中 The npm 中的超燃冲压发动机包 https www npmjs com package scramjet shows
  • 使用 React.js + Express.js 发送电子邮件

    我在 ES6 中使用 React js 构建了一个 Web 应用程序 我目前想要创建一个基本的 联系我们 页面并想要发送电子邮件 我是 React 新手 刚刚发现我实际上无法使用 React 本身发送电子邮件 我正在遵循教程nodemail
  • Node.js 重写 toString

    我试图覆盖我的对象的默认 toString 方法 这是代码和问题 function test this code 0 later on I will set these this name test prototype toString f
  • 如何在超级测试中模拟中间件?

    我想测试中间件是否在app js叫做 虽然我嘲笑该模块work js 它仍然运行原始代码 app js const work require work const express require require const app expr
  • Nodejs Express中间件函数返回值

    我正在使用 NodeJS 和 Express 我有以下路线 中间件功能是Mobile 如果我不使用 return next 在 isMobile 函数中 应用程序会卡住 因为 NodeJS 不会移至下一个函数 但我需要 isMobile 函

随机推荐