Skip to content

Commit 80b74f4

Browse files
Ian-ReadIan-Read
authored andcommitted
Wrapped events and added cheatsheet
1 parent 31df2af commit 80b74f4

File tree

3 files changed

+33
-17
lines changed

3 files changed

+33
-17
lines changed

cheatsheet/index.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Node Cheat Sheet
2+
3+
# <a name="Events"></a>Events
4+
5+
Class: events.EventEmitter
6+
7+
```
8+
emitter.addListener(event, listener)
9+
emitter.on(event, listener)
10+
emitter.once(event, listener)
11+
emitter.removeListener(event, listener)
12+
emitter.removeAllListeners([event])
13+
emitter.setMaxListeners(n)
14+
emitter.listeners(event)
15+
emitter.emit(event, [arg1], [arg2], [...])
16+
Event: 'newListener'
17+
```

events/index.md

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ layout: default.njk
33
title: Events
44
---
55

6+
# Events
7+
68
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.
79

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.
911

1012
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`.
1113

@@ -34,7 +36,9 @@ eventEmitter.emit('foo')
3436

3537
## Passing parameters
3638

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:
3842

3943
```
4044
const foo = function foo(bar) {
@@ -48,24 +52,16 @@ eventEmitter.on('foo', foo)
4852
eventEmitter.emit('foo', 'bar')
4953
```
5054

51-
## Objects:
52-
53-
```
54-
require('events)
55-
```
56-
57-
## Methods
58-
59-
`on(eventName, listenerFunction)`
60-
6155
## Summary
6256

63-
Main concept = listeners listen for events, that are emitted from emitters.
57+
Listeners _listen_ for events, that are _emitted_ from emitters.
6458

65-
## Exercise
59+
Take me to [cheat sheet](../cheatsheet/#events)
6660

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
6862

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.
7066

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.

index.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,6 @@ TODO:
99

1010
- Set out the purpose here and list the sections with links
1111
- Styling and update the default template
12+
13+
14+
[Events](/events)

0 commit comments

Comments
 (0)