diff options
author | Felipe Duitama <felipedc09@gmail.com> | 2019-06-22 01:37:37 +0300 |
---|---|---|
committer | Rich Trott <rtrott@gmail.com> | 2019-07-31 05:46:22 +0300 |
commit | 698d479aff2a71267a13ba1f3fda390221ea591e (patch) | |
tree | b546756e1773564c39097d1292fb2ed7062d470d /doc/api/events.md | |
parent | 9083a676dd3e01e01cc551993f0b0d7711acbbf1 (diff) |
doc: add example about emitter.emit in events documentation
PR-URL: https://github.com/nodejs/node/pull/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>
Diffstat (limited to 'doc/api/events.md')
-rw-r--r-- | doc/api/events.md | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/doc/api/events.md b/doc/api/events.md index 1e41b10b8c7..d102f8abcb7 100644 --- a/doc/api/events.md +++ b/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 |