Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is `emit` javascript function?

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?

like image 875
avasin Avatar asked Aug 27 '15 09:08

avasin


People also ask

What are event emitters in Javascript?

An event emitter is a pattern that listens to a named event, fires a callback, then emits that event with a value. Sometimes this is referred to as a “pub/sub” model, or listener.

What is emit in node JS?

The EventEmitter is a module that facilitates communication/interaction between objects in Node. EventEmitter is at the core of Node asynchronous event-driven architecture. Many of Node's built-in modules inherit from EventEmitter including prominent frameworks like Express. js.

What is EventEmitter used for?

Node. js uses events module to create and handle custom events. The EventEmitter class can be used to create and handle custom events module.

What is on () in node JS?

In Node. js, you usually bind functions (using 'on' or other functions) to listen to events.

What is the function of emit?

Short: Emit's job is to trigger named event (s) which in turn cause functions called listeners to be called. Detailed: Node.js core API is built around an idiomatic asynchronous event-driven architecture in which certain kinds of objects (called " emitters ") periodically emit named events that cause Function objects ("listeners") to be called.

What is the use of Event Emitter in JavaScript?

An interesting use of the event emitter is the so called “observer pattern”. Basically, there is a central event emitter object which has multiple attached “observer” objects. These observers are notified whenever the state of the central object is changed. This pattern can easily be implemented using events.

What does $emit () do in Vue?

The $emit Function in Vue. Aug 8, 2019. Vue components have a $emit () function that allows you to pass custom events up the component tree. Vue.component ('my-component', { mounted: function() { // `$emit ()` sends an event up the component tree.

What happens when the EventEmitter object emits an event?

When the EventEmitter object emits an event, all of the functions attached to that specific event are called synchronously. Any values returned by the called listeners are ignored and will be discarded. Please look at line number 624 of the same file. function emit (parser, event, data) { parser [event] && parser [event] (data) }


3 Answers

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)

like image 159
PsyLogic Avatar answered Oct 18 '22 03:10

PsyLogic


Short: Emit's job is to trigger named event(s) which in turn cause functions called listeners to be called.

Detailed: Node.js core API is built around an idiomatic asynchronous event-driven architecture in which certain kinds of objects (called "emitters") periodically emit named events that cause Function objects ("listeners") to be called.

All objects that emit events are instances of the EventEmitter class. These objects expose an eventEmitter.on() function that allows one or more functions to be attached to named events emitted by the object.

When the EventEmitter object emits an event, all of the functions attached to that specific event are called synchronously. Any values returned by the called listeners are ignored and will be discarded.

Read More here

like image 28
Hashmatullah Noorzai Avatar answered Oct 18 '22 01:10

Hashmatullah Noorzai


Please look at line number 624 of the same file.

function emit (parser, event, data) {

  parser[event] && parser[event](data)

}
like image 39
brk Avatar answered Oct 18 '22 01:10

brk