Node.js "on"函数 - 它有什么作用?

标签 node.js function

 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/

相关文章:

javascript - 删除 JSON 数组中特定索引的对象,不留下 "null"(Node.js)

php - WordPress:如何从 Json 中的 wordpress 函数获取响应?

c - 将函数及其大小传递给 WriteFile

node.js - jade 和客户端 javascript

javascript - 将 JS 属性设置为 object 或 null

javascript - JavaScript中如何将函数参数传递给回调函数?

c++ - C++是否保证参数评估的原子性?

javascript - 如何将此 AJAX 请求函数(原始 Javascript)转换为使用 POST 而不是 GET?

python - 有什么方法可以像 Python 或 PHP 一样在 NodeJS 中包含/需要文件吗?

AngularJS 拦截器没有将 JWT Bearer 放入 node.js 应用程序的每个请求中