What is `emit` javascript function?

avasin picture avasin · Aug 27, 2015 · Viewed 52.8k times · Source

While looking through sax nodejs module, i saw multiple emit function calls, but i can't find any information about it.

Is it some V8 native tool for emitting events? Why sax-js do not use EventEmitter for streams then?

Answer

PsyLogic picture PsyLogic · Aug 27, 2015

In node.js an event can be described simply as a string with a corresponding callback. An event can be "emitted" (or in other words, the corresponding callback be called) multiple times or you can choose to only listen for the first time it is emitted.

The on or addListener method (basically the subscription method) allows you to choose the event to watch for and the callback to be called. The emit method (the publish method), on the other hand, allows you to "emit" an event, which causes all callbacks registered to the event to 'fire', (get called).

reference: https://docs.nodejitsu.com/articles/getting-started/control-flow/what-are-event-emitters/ (This is an outdated link and doesn't work anymore)