Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.9k views
in Technique[技术] by (71.8m points)

readline 逐行同步读取, for await of 内如何判断是否读取完成?

const fRead = fs.createReadStream(file);
const rl = readline.createInterface({
    input: fRead,
});
for await (const row of rl) {
    // 请问这里如何判断是否最后一行?
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

看文档:http://nodejs.cn/api/readline...
你有两个问题

  • rl是readline.Interface的实例,要通过事件监听来读取,而不是用await + for of
  • 不存在最后一行这个说法,只存在流是否结束这个说法,只要流一直不结束就一直有可能会有新行出现。 通过监听close事件来判断流是否结束

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...