node.js - NodeJS EventEmitter 中事件的发送者

标签 node.js eventemitter

在 fork 多个子进程的 NodeJS 应用程序中,我需要知道子进程何时退出,问题是 EventEmitter 调用的回调函数没有事件的发送者。

我做过很多 .NET 开发,他们的事件总是有 sender 参数和 arguments 参数。

我如何知道哪个子进程发送了事件?

我可以做类似的事情

const childProcess = fork(...) childProcess.on('退出', 函数(代码, 信号) { myCallback(childProcess, 代码, 信号); }

但是我将无法从该特定事件处理程序中取消注册。

最佳答案

答案是this将被设置为引用发件人。

来源https://nodejs.org/api/events.html

The eventEmitter.emit() method allows an arbitrary set of arguments to be passed to the listener functions. It is important to keep in mind that when an ordinary listener function is called by the EventEmitter, the standard this keyword is intentionally set to reference the EventEmitter to which the listener is attached.

关于node.js - NodeJS EventEmitter 中事件的发送者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46759562/

相关文章:

javascript - 如何捕获在 'emit' 上运行异步函数的 eventEmitter 上的错误?

node.js - 在nodejs中发送多个具有不同主体的http请求

javascript - 如何在 RequireJS (AMD) 环境中访问 node.js 模块?

javascript - 如何处理 JavaScript 中的嵌套 Promise?

angularjs - 如何在浏览器中控制缓存

javascript - 这真的是从事件回调中获取 Rx.Observable 的最佳方式吗?

angular - 执行 EventEmitter 的不同方法 - Angular

javascript - 允许一个 Node 进程在其自己的模块之间进行通信的最适合扩展的方式是什么?

javascript - 将 ReactJS 与 PHP 和 Laravel 结合使用

javascript - 在 vuejs 上向父级发送样式值