Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Here we require in the 'events' module and save a reference to it in an events variable
- let events = require('events');
- // function which expects 'data' to be passed and will log a string to the console which incorporates that data
- let listenerCallback = (data) => {
- console.log('Celebrate ' + data);
- }
- // Create an instance of the EventEmitter class
- let myEmitter = new events.EventEmitter();
- // .on() method takes the name of the event (as a string) as its first argument & the listener callback function as its second argument
- myEmitter.on('celebration', listenerCallback);
- // .emit() method which announces a named event has occurred. Takes name of the event as its first argument & a string of your choice as the second argument
- myEmitter.emit('celebration', 'good times, come on! Here we go!');
- // Run program by typing node app.js onto the terminal
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement