Welcome to OStack Knowledge Sharing 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 Answer

0 votes
by (71.8m points)

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...