Node.js EventEmitter



NodeJS EventEmitter:

 

An action performed is termed as an event. Node.js is faster than other frameworks as it uses event driven programming technique. An event is more or less similar as a callback, the only difference is that the event handling works on the observer pattern while, the callback functions are called when an asynchronous function returns its result.

Node.js Events Module:

Events is a built-in module in Node.js which is used to create-, fire-, and listen an event.

Node.js EventEmitter Object:

The Node.js EventEmitter objects are used to assign event handlers to an event in two ways:

  • Return EventEmitter from a function
  • Extend the EventEmitter class

Syntax:

  • To include the Events module:

 var events = require(events);

  • To bind event and event listener:

 var eventEmitter = new events.EventEmitter();

  • To bind event handler with an event:

 eventEmitter.on (event_Name, eventHandler);

  • To fire an event:

 eventEmitter.emit (event_Name);

Example:

var events = require(‘events’);

var eventEmitter = new events.EventEmitter();

var Handle = function ()

{

console.log(‘I love Programming!’);

}

eventEmitter.on(‘coders’, Handle);

eventEmitter.emit(‘coders’);

 OUTPUT:

 I love Programming!

 

 

Tags: ,
Leave a comment

Your email address will not be published. Required fields are marked *

Subscribe now

Receive weekly newsletter with educational materials, new courses, most popular posts, popular books and much more!

https://bridgejunks.com/ https://crownmakesense.com/ https://brithaniabookjudges.com/ https://hughesroyality.com/ https://rhythmholic.com/ https://bandar89.simnasfikpunhas.com/ https://www.100calshop.co.il/products/thailand/ https://myasociados.com/ https://solyser.com/ http://konfidence.cz/ https://muscadinepdx.com/ https://bandar89.parajesandinos.com.ve/ https://goremekoop.com/ https://oncoswisscenter.com/ https://www.turunclifehotel.com/bandar89/ https://www.houseofproducts.biz/ https://taimoormphotography.com/
BIJI18 BIJI18 BIJI18