node.js - 使用 Node.JS Readline:“TypeError: rl is not async iterable”
问题描述
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: readline 模块现在支持异步迭代器。https://github.com/nodejs/node/pull/23916
如果您运行的是以前的版本,则必须升级。
推荐阅读
- r - 用 R 中的 lapply 系列替换双嵌套 for 循环
- go - 如何使用 OpenAPI Generator Client SDK 访问 HTTP 错误正文
- c# - 命令行参数和“字节”数组
- php - 将变量从一页传递到表单
- azure - 从 cosmosDB 文档的嵌套数组中删除数据
- python - 将类对象作为引用参数传递
- eclipse - 在 Eclipse 首选项中配置 maven 原型后,我的磁盘空间不足
- node.js - 如何从 mongodb 的子集合中获取数据?
- javascript - 如何使用 javascript/jquery 使用动态 json 数据无缝显示进度
- javascript - 捕获 FullCalendar4 点击日期数字事件