Skip to content
Permalink
Browse files

doc: add example about emitter.emit in events documentation

PR-URL: #28374
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Yongsheng Zhang <zyszys98@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
  • Loading branch information...
felipedc09 authored and Trott committed Jun 21, 2019
1 parent 9083a67 commit 698d479aff2a71267a13ba1f3fda390221ea591e
Showing with 34 additions and 0 deletions.
  1. +34 −0 doc/api/events.md
@@ -307,6 +307,40 @@ to each.

Returns `true` if the event had listeners, `false` otherwise.


```js
const EventEmitter = require('events');
const myEmitter = new EventEmitter();
// First listener
myEmitter.on('event', function firstListener() {
console.log('Helloooo! first listener');
});
// Second listener
myEmitter.on('event', function secondListener(arg1, arg2) {
console.log(`event with parameters ${arg1}, ${arg2} in second listener`);
});
// Third listener
myEmitter.on('event', function thirdListener(...args) {
const parameters = args.join(', ');
console.log(`event with parameters ${parameters} in third listener`);
});
console.log(myEmitter.listeners('event'));
myEmitter.emit('event', 1, 2, 3, 4, 5);
// Prints:
// [
// [Function: firstListener],
// [Function: secondListener],
// [Function: thirdListener]
// ]
// Helloooo! first listener
// event with parameters 1, 2 in second listener
// event with parameters 1, 2, 3, 4, 5 in third listener
```

### emitter.eventNames()
<!-- YAML
added: v6.0.0

0 comments on commit 698d479

Please sign in to comment.
You can’t perform that action at this time.