【问题标题】:With Node.JS Readline: "TypeError: rl is not async iterable"使用 Node.JS Readline:“TypeError: rl is not async iterable”
【发布时间】:2018-12-30 22:16:13
【问题描述】:

当我运行readline example中列出的代码时

async function processLineByLine() {
  const fileStream = fs.createReadStream('input.txt');

  const rl = readline.createInterface({
    input: fileStream,
    crlfDelay: Infinity
  });
  // Note: we use the crlfDelay option to recognize all instances of CR LF
  // ('\r\n') in input.txt as a single line break.

  for await (const line of rl) {
    // Each line in input.txt will be successively available here as `line`.
    console.log(`Line from file: ${line}`);
  }
}

processLineByLine();

我收到以下错误:

(node:27485) UnhandledPromiseRejectionWarning: TypeError: rl is not async iterable                                                                    
    at processLineByLine (file:///home/ecarroll/code/toponym-esque/process.mjs:16:28)                                                                 
    at file:///home/ecarroll/code/toponym-esque/process.mjs:22:1
    at ModuleJob.run (internal/modules/esm/module_job.js:95:12)
(node:27485) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)                                                     
(node:27485) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

【问题讨论】:

    标签: node.js readline


    【解决方案1】:

    对 readline 的更改是 Node v11.4.0 中的一项功能。

    readline: readline 模块现在支持异步迭代器。 https://github.com/nodejs/node/pull/23916

    如果您运行的是旧版本,则必须升级。

    【讨论】:

    • 注意:这个功能出现在 v10 LTS 文档 nodejs.org/docs/latest-v10.x/api/… 但是是的,它失败了。
    • 我让它在 Windows 上使用 10.16.2 版本。我有一位同事在 Mac 上运行 10.15.3,并让他更新到版本 10.18.0(我在我的 Mac 上的版本),它也可以工作。
    猜你喜欢
    • 2021-08-08
    • 1970-01-01
    • 2021-01-08
    • 1970-01-01
    • 2019-04-12
    • 2018-02-01
    • 2016-02-18
    • 1970-01-01
    • 2023-04-09
    相关资源
    最近更新 更多