简体   繁体   English

逐行读取行完成后执行Node.js

[英]Node.js Execute after Line-by-Line Readline Finishes

This likely would happen to other async functions, but I'm using readline as an example, as that's the issue I encountered. 其他异步函数可能会发生这种情况,但是我以readline为例,因为这是我遇到的问题。

I've just started using JS and is currently trying to collect input from a file via stdin( < ). 我刚刚开始使用JS,目前正在尝试通过stdin( < )从文件中收集输入。 An example would be the code below: 下面的代码就是一个例子:

const readline = require('readline');
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout,
    terminal: false
});

rl.on('line', foo);

function foo(line) {
    // Code to be iterated by line
}

function summary() {
    // Code to be executed after rl finishes
}

So the question is, where do I put summary function? 所以问题是,我应该将summary函数放在哪里? Does rl.on take another callback that is executed after it finishes? rl.on是否接受另一个在完成后执行的回调?

Clarification: foo is called each time when rl reads a line and summary is called only once, which is after rl finishes 说明:每次rl读取一行时都会调用foo ,而rl完成后仅会调用一次summary

您可能正在寻找关闭事件

rl.on('close', summary);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM