-
I am a bit confused by the documentation for Event Target. In browsers you can create custom events. Can you do that in Node? I couldn't find anything relevant from googling. I tried reading through this blog post from NearForm but it uses a "click" event as an example, which obviously is not an event that would ever happen in Node.js so needs to be artificially dispatched, which just confuses me even more as to why you would ever use a fake click event in Node. Also, because Event Target is very similar to Event Emitter but added later, I have the question of whether Event Emitter is still what I should use most of the time, or whether Event Target can replace it? There aren't many resources/guides/blog posts in general about Event Target in Node and reading MDN is very browser-focused. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Yes, you can create your own custom Event objects such as... class FooEvent extends Event {
constructor() { super('foo'); }
}
const et = new EventTarget();
et.dispatchEvent(new FooEvent()) With regards to EventTarget and EventEmitter, they definitely are very difficult things. The decision of which one to use depends on a number of factors. If your application is already heavily using EventEmitter, then I would just stick with that. If you have the option of designing or controlling the entire API and wish to have an approach that is compatible with browsers, deno, workers or other environments, then EventTarget would be ideal. |
Beta Was this translation helpful? Give feedback.
Yes, you can create your own custom Event objects such as...
With regards to EventTarget and EventEmitter, they definitely are very difficult things. The decision of which one to use depends on a number of factors. If your application is already heavily using EventEmitter, then I would just stick with that. If you have the option of designing or controlling the entire API and wish to have an approach that is compatible with browsers, deno, workers or other environments, then EventTarget would be ideal.