var rd = readline.createInterface({
input: fs.createReadStream(file),
output: process.stdout,
terminal: false
});
rd.on('line', function(line) {
有什么方法可以使“on”工作同步。 “on”循环内的代码需要在再次执行循环之前完成。有什么办法可以做到这一点吗?
最佳答案
通常,on
函数会为特定事件注册事件处理程序。
readline::line
事件就是为了这个目的:
Emitted whenever the input stream receives a \n, usually received when the user hits enter, or return. This is a good hook to listen for user input.
— NodeJS documentation
顺便问一下,您使用哪些关键字进行搜索?我使用了nodejs readline.createInterface
,第一个结果是这个 NodeJS 文档页面!
关于Node.js "on"函数 - 它有什么作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17379050/