From the course: Node.js Essential Training

Unlock the full course today

Join today to access over 24,300 courses taught by industry experts.

Consuming a module with EventEmitter

Consuming a module with EventEmitter - Node.js Tutorial

From the course: Node.js Essential Training

Consuming a module with EventEmitter

- [Instructor] Let's make some adjustments to our collectAnswers function. And the first thing we want to do here is we want to require the EventEmitter and we're going to destructure this from the events module. So I only want to use event emitter. I don't have to use events.EventEmitter every time. Once I've imported this, I can make some adjustments to our function. The first thing we want to do is include the emitter which will be a new instance of the EventEmitter. And we'll call that there. And then down here at the end of our function, we want to return the emitter so that we can use it. 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. What we can do is say emitter.emit answer with whatever that answer is. So what we can do now over in our ask function, we want to be able to…

Contents