node.js - 使用 Node.JS Readline : "TypeError: rl is not async iterable"

标签 node.js readline

当我运行 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.

最佳答案

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

readline: The readline module now supports async iterators. https://github.com/nodejs/node/pull/23916

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

关于node.js - 使用 Node.JS Readline : "TypeError: rl is not async iterable",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53981871/

相关文章:

c# - 使用分隔符读取文本文件中的所有行

logging - 更改 PHP 交互式 shell 历史记录的保存路径

python-3.x - 安装readline后python 3.6崩溃

python - python cmd 模块中的持久历史记录

node.js - 从URL获取对象属性值

angularjs - Node : npm install command shows error

node.js - BLE 连接缓冲区大小与数据包长度

python - 如果使用颜色提示,如何修复 Python readline 中的列计算

javascript - 是否可以将渲染函数放入 Express 的 for 循环中?

javascript - 如何在 https 请求中设置 session 或 cookie 到 url?