Node.js Events
Every action on a computer is an event. Node.js object can fire an event like readStream object fires events when opening and closing a file.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Every action on a computer is an event. Node.js object can fire an event like readStream object fires events when opening and closing a file.
var fs = require('fs');
var rs = fs.createReadStream('./demo.txt');
rs.on('open', function () {
console.log('The file is open');
});
Events Module
Node.js has a built-in module, called "Events", where you can create-, fire-, and listen for- your own events. All event properties and methods of event module are an instance of an EventEmitter object.
EventEmitter Object
var events = require('events');
var eventEmitter = new events.EventEmitter();
//Create an event handler:
var myEventHandler = function () {
console.log('I hear a scream!');
}
//Assign the event handler to an event:
eventEmitter.on('scream', myEventHandler);
//Fire the 'scream' event:
eventEmitter.emit('scream');
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Comments
Post a Comment