@@ -15,11 +15,11 @@ a [stream][] emits an event whenever data is available to be read.
15
15
16
16
All objects that emit events are instances of the ` EventEmitter ` class. These
17
17
objects expose an ` eventEmitter.on() ` function that allows one or more
18
- Functions to be attached to named events emitted by the object. Typically,
18
+ functions to be attached to named events emitted by the object. Typically,
19
19
event names are camel-cased strings but any valid JavaScript property key
20
20
can be used.
21
21
22
- When the ` EventEmitter ` object emits an event, all of the Functions attached
22
+ When the ` EventEmitter ` object emits an event, all of the functions attached
23
23
to that specific event are called _ synchronously_ . Any values returned by the
24
24
called listeners are _ ignored_ and will be discarded.
25
25
@@ -109,7 +109,8 @@ myEmitter.emit('event');
109
109
```
110
110
111
111
Using the ` eventEmitter.once() ` method, it is possible to register a listener
112
- that is unregistered before it is called.
112
+ that is called at most once for a particular event. Once the event is emitted,
113
+ the listener is unregistered and * then* called.
113
114
114
115
``` js
115
116
const myEmitter = new MyEmitter ();
@@ -126,7 +127,7 @@ myEmitter.emit('event');
126
127
## Error events
127
128
128
129
When an error occurs within an ` EventEmitter ` instance, the typical action is
129
- for an ` 'error' ` event to be emitted. These are treated as a special case
130
+ for an ` 'error' ` event to be emitted. These are treated as special cases
130
131
within Node.js.
131
132
132
133
If an ` EventEmitter ` does _ not_ have at least one listener registered for the
@@ -139,10 +140,9 @@ myEmitter.emit('error', new Error('whoops!'));
139
140
// Throws and crashes Node.js
140
141
```
141
142
142
- To guard against crashing the Node.js process, developers can either register
143
- a listener for the ` process.on('uncaughtException') ` event or use the
144
- [ ` domain ` ] [ ] module (_ Note, however, that the ` domain ` module has been
145
- deprecated_ ).
143
+ To guard against crashing the Node.js process, a listener can be registered
144
+ on the [ ` process ` object's ` uncaughtException ` event] [ ] or the [ ` domain ` ] [ ] module
145
+ can be used. (_ Note, however, that the ` domain ` module has been deprecated_ )
146
146
147
147
``` js
148
148
const myEmitter = new MyEmitter ();
@@ -155,8 +155,7 @@ myEmitter.emit('error', new Error('whoops!'));
155
155
// Prints: whoops! there was an error
156
156
```
157
157
158
- As a best practice, developers should always register listeners for the
159
- ` 'error' ` event:
158
+ As a best practice, listeners should always be added for the ` 'error' ` events.
160
159
161
160
``` js
162
161
const myEmitter = new MyEmitter ();
@@ -176,15 +175,15 @@ const EventEmitter = require('events');
176
175
```
177
176
178
177
All EventEmitters emit the event ` 'newListener' ` when new listeners are
179
- added and ` 'removeListener' ` when a listener is removed.
178
+ added and ` 'removeListener' ` when existing listeners are removed.
180
179
181
180
### Event: 'newListener'
182
181
183
182
* ` eventName ` {String|Symbol} The name of the event being listened for
184
183
* ` listener ` {Function} The event handler function
185
184
186
- The ` EventEmitter ` instance will emit it's own ` 'newListener' ` event * before*
187
- a listener is added to it's internal array of listeners.
185
+ The ` EventEmitter ` instance will emit its own ` 'newListener' ` event * before*
186
+ a listener is added to its internal array of listeners.
188
187
189
188
Listeners registered for the ` 'newListener' ` event will be passed the event
190
189
name and a reference to the listener being added.
@@ -219,7 +218,7 @@ myEmitter.emit('event');
219
218
* ` eventName ` {String|Symbol} The event name
220
219
* ` listener ` {Function} The event handler function
221
220
222
- The ` 'removeListener' ` event is emitted * after* a listener is removed.
221
+ The ` 'removeListener' ` event is emitted * after* the ` listener ` is removed.
223
222
224
223
### EventEmitter.listenerCount(emitter, eventName)
225
224
@@ -251,7 +250,7 @@ precedence over `EventEmitter.defaultMaxListeners`.
251
250
252
251
Note that this is not a hard limit. The ` EventEmitter ` instance will allow
253
252
more listeners to be added but will output a trace warning to stderr indicating
254
- that a ` possible EventEmitter memory leak ` has been detected. For any single
253
+ that a " possible EventEmitter memory leak" has been detected. For any single
255
254
` EventEmitter ` , the ` emitter.getMaxListeners() ` and ` emitter.setMaxListeners() `
256
255
methods can be used to temporarily avoid this warning:
257
256
@@ -301,7 +300,7 @@ set by [`emitter.setMaxListeners(n)`][] or defaults to
301
300
302
301
### emitter.listenerCount(eventName)
303
302
304
- * ` eventName ` {Value } The name of the event being listened for
303
+ * ` eventName ` {String|Symbol } The name of the event being listened for
305
304
306
305
Returns the number of listeners listening to the event named ` eventName ` .
307
306
@@ -319,7 +318,7 @@ console.log(util.inspect(server.listeners('connection')));
319
318
320
319
### emitter.on(eventName, listener)
321
320
322
- * ` eventName ` {string |Symbol} The name of the event.
321
+ * ` eventName ` {String |Symbol} The name of the event.
323
322
* ` listener ` {Function} The callback function
324
323
325
324
Adds the ` listener ` function to the end of the listeners array for the
@@ -334,7 +333,7 @@ server.on('connection', (stream) => {
334
333
});
335
334
```
336
335
337
- Returns a reference to the ` EventEmitter ` so calls can be chained.
336
+ Returns a reference to the ` EventEmitter ` , so that calls can be chained.
338
337
339
338
By default, event listeners are invoked in the order they are added. The
340
339
` emitter.prependListener() ` method can be used as an alternative to add the
@@ -352,7 +351,7 @@ myEE.emit('foo');
352
351
353
352
### emitter.once(eventName, listener)
354
353
355
- * ` eventName ` {string |Symbol} The name of the event.
354
+ * ` eventName ` {String |Symbol} The name of the event.
356
355
* ` listener ` {Function} The callback function
357
356
358
357
Adds a ** one time** ` listener ` function for the event named ` eventName ` . The
@@ -364,7 +363,7 @@ server.once('connection', (stream) => {
364
363
});
365
364
```
366
365
367
- Returns a reference to the ` EventEmitter ` so calls can be chained.
366
+ Returns a reference to the ` EventEmitter ` , so that calls can be chained.
368
367
369
368
By default, event listeners are invoked in the order they are added. The
370
369
` emitter.prependOnceListener() ` method can be used as an alternative to add the
@@ -382,7 +381,7 @@ myEE.emit('foo');
382
381
383
382
### emitter.prependListener(eventName, listener)
384
383
385
- * ` eventName ` {string |Symbol} The name of the event.
384
+ * ` eventName ` {String |Symbol} The name of the event.
386
385
* ` listener ` {Function} The callback function
387
386
388
387
Adds the ` listener ` function to the * beginning* of the listeners array for the
@@ -397,11 +396,11 @@ server.prependListener('connection', (stream) => {
397
396
});
398
397
```
399
398
400
- Returns a reference to the ` EventEmitter ` so calls can be chained.
399
+ Returns a reference to the ` EventEmitter ` , so that calls can be chained.
401
400
402
401
### emitter.prependOnceListener(eventName, listener)
403
402
404
- * ` eventName ` {string |Symbol} The name of the event.
403
+ * ` eventName ` {String |Symbol} The name of the event.
405
404
* ` listener ` {Function} The callback function
406
405
407
406
Adds a ** one time** ` listener ` function for the event named ` eventName ` to the
@@ -414,7 +413,7 @@ server.prependOnceListener('connection', (stream) => {
414
413
});
415
414
```
416
415
417
- Returns a reference to the ` EventEmitter ` so calls can be chained.
416
+ Returns a reference to the ` EventEmitter ` , so that calls can be chained.
418
417
419
418
### emitter.removeAllListeners([ eventName] )
420
419
@@ -424,7 +423,7 @@ Note that it is bad practice to remove listeners added elsewhere in the code,
424
423
particularly when the ` EventEmitter ` instance was created by some other
425
424
component or module (e.g. sockets or file streams).
426
425
427
- Returns a reference to the ` EventEmitter ` so calls can be chained.
426
+ Returns a reference to the ` EventEmitter ` , so that calls can be chained.
428
427
429
428
### emitter.removeListener(eventName, listener)
430
429
@@ -485,10 +484,10 @@ myEmitter.emit('event');
485
484
Because listeners are managed using an internal array, calling this will
486
485
change the position indices of any listener registered * after* the listener
487
486
being removed. This will not impact the order in which listeners are called,
488
- but it will means that any copies of the listener array as returned by
487
+ but it means that any copies of the listener array as returned by
489
488
the ` emitter.listeners() ` method will need to be recreated.
490
489
491
- Returns a reference to the ` EventEmitter ` so calls can be chained.
490
+ Returns a reference to the ` EventEmitter ` , so that calls can be chained.
492
491
493
492
### emitter.setMaxListeners(n)
494
493
@@ -499,12 +498,13 @@ The `emitter.setMaxListeners()` method allows the limit to be modified for this
499
498
specific ` EventEmitter ` instance. The value can be set to ` Infinity ` (or ` 0 ` )
500
499
to indicate an unlimited number of listeners.
501
500
502
- Returns a reference to the ` EventEmitter ` so calls can be chained.
501
+ Returns a reference to the ` EventEmitter ` , so that calls can be chained.
503
502
504
503
[ `net.Server` ] : net.html#net_class_net_server
505
504
[ `fs.ReadStream` ] : fs.html#fs_class_fs_readstream
506
505
[ `emitter.setMaxListeners(n)` ] : #events_emitter_setmaxlisteners_n
507
506
[ `EventEmitter.defaultMaxListeners` ] : #events_eventemitter_defaultmaxlisteners
508
507
[ `emitter.listenerCount()` ] : #events_emitter_listenercount_eventname
509
508
[ `domain` ] : domain.html
509
+ [ `process` object's `uncaughtException` event ] : process.html#process_event_uncaughtexception
510
510
[ stream ] : stream.html
0 commit comments