FAQ: Learn Node.js - Event-Driven Architecture

This community-built FAQ covers the “Event-Driven Architecture” exercise from the lesson “Learn Node.js”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Node.js

FAQs on the exercise Event-Driven Architecture

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

What exactly is a ‘celebration’ event, and how does Node recognize that one has happened when I run the app from terminal?

3 Likes

Same question here. Unfortunately I don’t understand the concept of emitting and switching the event listeners in node.

1 Like

I have the same question. The instruction in this lesson is rather terse. And it’s ridiculous that no one has answered the question for the past five months. CodeCademy’s quality of education has been deteriorating…

Here’s my guess on the answer. The “celebration” event appears to be defined by the .emit() method. Whenever you call the .emit() method, an event whose name is defined as its first argument (i.e. ‘celebration’), is triggered. Then the .on() method picks it up and executes the function defined as its second argument (i.e. listenerCallback) with an argument defined in the second argument of the .emit() method (i.e. a string you have chosen).

Can someone confirm that my understanding above is correct?

2 Likes

BUMP! It seems like more attention is paid to earlier exercises. Maybe less users get this far and CA doesn’t feel the need to address questions this far in. Too bad I already paid for a full year!

1 Like

Very good and correct explanation :slight_smile:


I am sorry to hear that. This forum is driven by the community. It works because there are people out there who like to help others. There might be many questions that are left unanswered but there is a lot more questions that got deep, mindfull answers. And this is quite amazing given that these questions were answered by regular users like you or me.

The communities built around some topics are small because some courses simply are not that popular. Percentage of users that are getting back to the forums to answer some questions, to help other learners is relatively small. It’s not about Codecademy not feeling responsible for parts of the content, this is a natural mechanism of communities - categories that attract many people are more active.


I can only hope that you guys will find some time to help others struggling with the earlier parts of the course. Especially since, as you noticed, there is a lack of activity in these categories.

5 Likes

This lesson doesn’t explain much about what events are used for. What’s the use of using the EventEmitter calling back a function when you can just directly put the data as a parameter of the function itself?

From what I’ve googled, events are used for asynchronous “events.” These are events that could be “triggered” and executed at the same time, without waiting for prior events to finish first. I only have a vague idea on how it is actually used in practice, which I hoped I could learn here in CA.

2 Likes

@factoradic cheers
a question though, why is it when i console.log the new myEmitter variable or even the EventEmitter object, I can’t find either the .on or the .emit method?

Hi! This might sound really dumb, but what’s the difference between .on() method and .addListener method on the events object? Also, can I think of .emit() as a method to define a new event object instance? Thanks!

According to the documentation, emitter.addListener() is an alias for emitter.on(), so there is no difference – they’re exactly the same thing.

I also think of the emitter.emit() method a little differently. Here’s an analogy for an event-driven system using basketball:

let events = require('events');
let score = 0;
const scoreKeeper= new events;

const updateScoreBoard = (points) => {score += points};

scoreKeeper.on('make_basket', updateScoreBoard);

const shoot_a_basket = (shot, points) => {
  if (shot) {
    scoreKeeper.emit('make_basket', points);
  }
}

Here, we have a scoreKeeper.on() event-listener listening for an event that would indicate a basket being made. Each scoring attempt is a call of the shoot_a_basket function. If the ball goes in the net (indicated by a true value for shot), scoreKeeper.emit() is called, which alerts all event-listeners listening for the make_basket event announcement to run their callback function, and passes the value of the basket made.

As you can see, the purpose of .emit() is to let all event-listeners waiting for a particular event to happen know that the event has happened when called. I think of it more as an announcer.

3 Likes

You can’t enumerate an object that way. Try for…in instead.

Example:

let events = require('events');
const myEmitter = new events;

for (let property in myEmitter) {
  console.log(property);
}

/*
Output:

_events
_eventsCount
_maxListeners
setMaxListeners
getMaxListeners
emit
addListener
on
prependListener
once
prependOnceListener
removeListener
off
removeAllListeners
listeners
rawListeners
listenerCount
eventNames
*/

So this to me looks a lot like using an event listener on the dom with javascript so i got a theory and used:

let events = require(‘events’);
let listenerCallback = (data) => {
console.log('Celebrate ’ + data);
}
let myEmitter = new events.EventEmitter();
myEmitter.emit(listenerCallback(‘Good times come on!’));

to try refractor is as it makes no sense why

myEmitter.on(‘celebration’, listenerCallback);

that is there it seems unnecessary and just extra work for the same result
My theory worked so im trying to understand why the part i think is unnecessary is there, what am i missing ?

It just seems like an overly complicated way to add an argument to a function

hello, so playing with this and found that .on() defines the event, and .emit() invokes it. See my example below and change the value of your first argument in the .on() variable to match any of the strings in the .emit(). You will see that the data passed depends on that value. I hope this helps:

let events = require('events');

let listenerCallback = (data) => {
    console.log('Celebrate ' + data);
}

const myEmitter = new events.EventEmitter();

myEmitter.on('celebration1', listenerCallback);  //change my 1st arg to match any one of the emit() below

myEmitter.emit('celebration1', 'Number 1 event emitter');
myEmitter.emit('celebration2', 'Number 2 event emitter');
myEmitter.emit('celebration3', 'Number 3 event emitter');