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 trialLuis Martín
1,439 PointsWhat are the implications of writing this code within profile.js?
This question doesn't exactly keep to the code seen in this course, but to the code within profile.js, which is responsible to connect to the teamtreehouse API and get the user profile data.
Right at the beginning of the Profile function, I see these two lines:
EventEmitter.call(this);
var profileEmitter = this;
My question is, what difference does it make if I instead do something easier to grasp like:
var profileEmitter = EventEmitter;
The end result is just the same, and there's no other code which benefits from changing the context within Profile to EventEmitter.
2 Answers
Seth Kroger
56,414 PointsIf you use the global EventEmitter instead of calling it as a parent constructor for each new Profile you can run into concurrency issues. If you have more than one Profile request waiting for a response back, you want the events separate for each profile, not mixed up.
Luis Martín
1,439 PointsYou mean that by doing those actions each instance of Profile will reference a different instance of EventEmitter? What would be the problem in using the same one?
Also I can't figure out why those actions, that to my understanding represent to give the global EventEmitter the context of the profile, will create different event emitters. Seems too confusing.