Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialVinay Rajagopal
2,471 PointsWhat is EventEmitter?
I'm looking at profile.js and I am trying to figure out what EventEmitter is used for. To me, it just looks like another way to do console.log, but can someone please clarify?
Thanks. Here is my code for reference:
var EventEmitter = require("events").EventEmitter; var https = require("https"); var http = require("http"); var util = require("util");
/**
- An EventEmitter to get a Treehouse students profile.
- @param username
-
@constructor */ function Profile(username) {
EventEmitter.call(this);
var profileEmitter = this; var url = 'https://teamtreehouse.com/'+ username + '.json';
var request = https.get(url, function(response) { var body = ""; // aggregates the chunks if (response.statusCode !== 200) { //Status Code Error request.abort(); profileEmitter.emit("error", new Error("There was an error getting the profile for " + username + ". (" + http.STATUS_CODES[response.statusCode] + ")")); } //Read the data response.on('data', function (chunk) { body += chunk; // profileEmitter.emit("data", chunk); }); response.on('end', function () { if(response.statusCode === 200) { try { var profile = JSON.parse(body); //Parse the data profileEmitter.emit("end", profile); } catch (error) { profileEmitter.emit("error", error); } } }).on("error", function(error){ profileEmitter.emit("error", error); }); }); }
util.inherits( Profile, EventEmitter );
module.exports = Profile;
1 Answer
Thomas Nilsen
14,957 PointsHere is a fairly simple implementation of an EventEmitter:
function EventEmitter() {
//An object containing all the different events
this.events = {};
}
EventEmitter.prototype.on = function(type, callback) {
//We check if the key exists -
//If not we create it, and set it equal to an empty array
this.events[type] = this.events[type] || [];
//Add our callback to the array
this.events[type].push(callback);
}
EventEmitter.prototype.emit = function(type) {
//Does the key exists
if(this.events[type]) {
//Loops over the array and call every function that is stored in it.
this.events[type].forEach(function(func) {
func();
});
}
}
var e = new EventEmitter();
//Here we create an event to look out for, and add the function that will be called
e.on('entry', function() {
console.log('hello');
});
//This outputs 'hello'
e.emit('entry');
Basically you can use it to communicate different events across your app, and react accordingly.