- [Instructor] Back in lesson five of the current chapter,
we took our collectAnswers function
and we turned it into a module
that we can make reusable.
So if you look within your exercise files
under the lib folder, there's a collectAnswers.js,
and this is our module for the collectAnswers function.
We can improve this function by using the EventEmitter.
So I'm going to go ahead and pull in my EventEmitter
by calling const EventEmitter.
Notice this time, I'm destructuring the EventEmitter
right when I require our events module.
That way I can just use it directly
without having to preface it with events dot.
So down here inside of our function,
let's go ahead and create an instance of our emitter.
So we'll create an emitter equals new EventEmitter,
so we're creating an instance of an EventEmitter
by invoking the constructor function.
And then down here at the very end of the function,
let's go ahead and return our emitter.
So now we can use this emitter to raise events
when certain things happen within this function.
For example, let's say we wanted to raise an event
every time the user answered a question.
Well, whenever the users answer a question,
this question answered function gets fired,
so right before we push the answer in the array,
I can call emitter.emit and we can emit an answer event.
So go ahead and emit event,
and we can push the answer that was returned.
Great, so I can go ahead and save this.
Now every time the user answers a question,
we're raising an event.
Let's also raise one more event.
Down here where we say done
and use the callback, let's go ahead
and emit a complete event.
And when we emit the complete event,
we will go ahead and pass all of the answers.
This just provides an additional option
for our consumer to listen to complete events.
Great, so we've improved our function.
Let's go ahead and save this,
and come out to our consumer module
which is the questions.js.
And the first thing that we should notice is
if we actually open up the terminal,
and I run this file, it still works.
So we can see that everything still works,
we've just improved this function
by providing some options.
And those options are to wire up listeners for events.
So for instance, I can come in here,
and collectAnswers returns an EventEmitter
if we want to use it.
So I can say const answerEvents equal,
and then there's our collectAnswers function,
and then I can come in here and say answerEvents dot on.
Whenever there's an answer, we can take that answer,
and we can just go ahead and log it to the console.
And I'll go ahead and collapse the file
so we can see the whole thing.
And we'll go ahead and add our answer
to our question answered, great!
So now whenever there's an answerEvent, we'll go ahead
and add the question answered to the console.
So let's go ahead and take a look at this now,
and see what that change has done.
So I'm going to clear the current screen.
Node our questions, and we say what is your name,
but as soon as I answer the question,
I have an additional handler there,
and that's because our collectAnswers function
now raises an answer event.
So if I say where do you live,
and what do you want to do with node.js,
we're just repeating all of the answers back
as they occur to the user using the EventEmitter.
I can also improve my code by adding a complete event.
So we'll say answerEvents dot on,
and complete is the event that we'll listen for.
And what we want to do
within this complete event is I'm going to go ahead
and take our callback function here,
so we will do this as opposed to using the done function,
and I'm just going to go ahead and paste
that as the second argument.
So when we are completed, we're going
to go ahead and get answers.
And to show that we can wire up as many of these
as we'd like, I'm going to go ahead
and grab that process.exit, and do another answerEvent.
So this time when we're complete,
I'm just going to go ahead and invoke a function
that calls the process.exit.
There we go.
So notice, I have two listeners for the complete event.
Now when we're finished, we're going to say thank you
for your answer and log the answers,
but also we are going to exit the process.
So let's go ahead and open up our terminal,
and watch these changes.
So I'll go ahead and clear the current stuff
from the terminal.
And say node questions.
So now we get what is your name?
Alex.
Question answered.
Tahoe.
And what are you going to do with node.js?
Change the world.
And now we can see that our questions answered,
thank you for our answers,
and the process has exited.
So now we've improved our collectAnswers function
by giving our consumers more options.
So they can wire up as many listeners
as they want to handle all sorts of events
as our user is answering questions.