You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: events/index.md
+13-17Lines changed: 13 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,9 +3,11 @@ layout: default.njk
3
3
title: Events
4
4
---
5
5
6
+
# Events
7
+
6
8
The Node.js core API is built around the idea of events being "emitted" and "listened" to. Objects called "emitters" emit _named_ events, that are picked up by "listener" functions.
7
9
8
-
Objects that emit events extend the `EventEmitter` class. These objects expose an eventEmitter.on() function that allows one or more functions to be attached to named events emitted by the object.
10
+
Objects that emit events extend the `EventEmitter` class. These objects expose an `on` method that allows one or more functions to be attached to named events emitted by the object.
9
11
10
12
When the EventEmitter object emits an event, all of the functions attached to that specific event are called _synchronously_, in the order they were added. This is to prevent race conditions and logic errors. They can switch to an async mode using `setImmediate`.
11
13
@@ -34,7 +36,9 @@ eventEmitter.emit('foo')
34
36
35
37
## Passing parameters
36
38
37
-
When an event is emitted using the `emit` method, the subsequent arguments are passed through to the listeners. For example:
39
+
When an event is emitted using the `emit` method, the subsequent arguments are passed through to the listeners.
40
+
41
+
For example:
38
42
39
43
```
40
44
const foo = function foo(bar) {
@@ -48,24 +52,16 @@ eventEmitter.on('foo', foo)
48
52
eventEmitter.emit('foo', 'bar')
49
53
```
50
54
51
-
## Objects:
52
-
53
-
```
54
-
require('events)
55
-
```
56
-
57
-
## Methods
58
-
59
-
`on(eventName, listenerFunction)`
60
-
61
55
## Summary
62
56
63
-
Main concept = listeners listen for events, that are emitted from emitters.
57
+
Listeners _listen_for events, that are _emitted_ from emitters.
64
58
65
-
## Exercise
59
+
Take me to [cheat sheet](../cheatsheet/#events)
66
60
67
-
Image we are building a SaaS that does a number of things when a user creates an account. functionality that sends emails "confirmation" to a user when they create an account.
61
+
## Exercise
68
62
69
-
There is a mock emailer class in the folder that has a method `send`, which expects an email address and a message body as the parameters.
63
+
Imagine we are building a SaaS that does a number of things when a user creates an account. When a user record
64
+
is created, we want to emit a `userCreated` event. One of the listeners for this will email a confirmation
65
+
email to that user.
70
66
71
-
Use this to send a welcome email when a `userCreated` event is fired.
67
+
Build an event emitter to simulate the `userCreated` event, and an event listener that sends a confirmation email. There is a mock emailer class in the folder that has a method `send`, which expects an email address and a message body as the parameters.
0 commit comments