Skip to content

Custom events in Node? Event Target confusion #39861

Answered by jasnell
o-t-w asked this question in General
Discussion options

You must be logged in to vote

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.

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by o-t-w
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
eventtarget Issues and PRs related to the EventTarget implementation.
2 participants