Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Emit and Listen to Events

In this lesson, you gave washer 1 a way to tell two housemates that its cycle had ended, without the washer knowing who was listening, and saw exactly when each of them hears it.

Key Points

  • A callback tells one function when something is done. Washer 1 has to tell two people, Ines and Kofi, and it shouldn’t have to know who’s listening.
  • Node’s EventEmitter class, from the node:events module, makes an object that announces events. You import it as you would from any module, and new EventEmitter() makes the washer.
  • washer.on("done", listener) adds a listener for the "done" event: a callback to run when that event happens. A second .on() call for the same event adds a second listener.
  • Adding a listener doesn’t run it. Nothing has happened to the washer yet.
  • washer.emit("done", "Ines") announces the "done" event. Whatever you pass after the event’s name goes to every listener, so each listener’s name parameter gets "Ines".
  • emit() calls each listener itself, on the same call stack, one after the other, in the order you added them. Only when both listeners have finished does emit() return, and the line after it runs.
  • emit() never goes near the task queue. In the script below, the timer is what waited: the event loop moved the cycle callback onto the empty stack, and everything after that ran in that one task.
  • The washer doesn’t remember its events. A listener you add once the washer has already emitted is too late, and it never hears that emit() call.

The rule: emit() calls every listener for its event right away, in the order you added them, before the next line runs.

The Final Code

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 In the last exercise, a callback told your program when each lookup was done. In this video, you’ll see how a machine can tell several parts of a program that it’s finished, and exactly when each part hears it.

00:14 Ines has started washer 1. When its cycle ends, Ines gets a message to collect her laundry. And Kofi, who’s waiting for a free washer, gets a message too. With a callback, the washer could call back one function.

00:28 Here, it needs to tell 2, and it shouldn’t have to know who’s listening.

00:34 Node has a class for this, EventEmitter, in its events module. You import it as you would from any module, and new makes a washer from it.

00:44 The washer has a method called .on(). It takes an event’s name, and here the event is called "done". Then it takes a callback to run when that event happens.

00:55 That callback is called a listener. When it runs, it gets the housemate’s name, and messages them. A second listener for the same event messages Kofi.

01:06 Adding a listener doesn’t run it. Nothing has happened to the washer yet.

01:12 The cycle is a toy timer, 300 milliseconds. When the cycle callback runs, it calls the washer’s emit() method, which announces the "done" event.

01:22 Whatever you pass after the event’s name goes to every listener. Here, that’s Ines’s name. Then the cycle callback logs that the washer has told everyone. The script’s last line logs that washer 1 is running.

01:36 Run it. The first line is the script’s own, so it prints now. The other 3 wait for the timer. When the cycle callback runs, in what order do they print? Your turn: pause the video and predict the order.

01:57 Ines’s message prints first, then Kofi’s, and only then the line saying washer 1 has told everyone. The line after emit() came last, after both listeners.

02:09 Here’s why, on the picture from earlier in the course.

02:13 Once the timer is due and the stack is empty, the event loop moves the cycle callback onto the call stack. The callback calls emit(), which goes on top. emit() calls the first listener itself, on the same stack.

02:27 It prints Ines’s message and leaves. Then emit() calls the second listener, in the order you added them. Only when both have finished does emit() return, and the callback’s next line runs.

02:40 emit() never goes near the task queue. The timer is what waited. emit() itself is a function call that calls each listener, one after the other.

02:52 So here’s the rule: emit() calls every listener for its event right away, in the order you added them, before the next line runs. Watch out, though.

03:03 The washer doesn’t remember its events. A listener you add once the washer has already emitted is too late, and it never hears that emit() call.

03:13 So far, washer 1 is a plain EventEmitter, and your own code calls emit() on it. Next, in Build a Machine That Emits Events, you’ll write a washer class that emits "done" itself.

Become a Member to join the conversation.