Skip to content

Instantly share code, notes, and snippets.

@kenriortega
Created May 16, 2021 13:57
Show Gist options
  • Save kenriortega/507024597b719823b8c32bfb475f5e53 to your computer and use it in GitHub Desktop.
Save kenriortega/507024597b719823b8c32bfb475f5e53 to your computer and use it in GitHub Desktop.
Class # 1 Event Emitter

Event emitter

const { EventEmitter } = require('events')
const ee = new EventEmitter()

setInterval(() => {
  ee.emit('ping', ` [${Date.now()}]: wake up`)
}, 3000)
module.exports = ee
'use strict'
const ee = require('./ee')

ee.on('ping', (value) => {
  console.log(`Listener 1, recived:${value}`)
})
ee.on('ping', (value) => {
  console.log(`Listener 2, recived:${value}`)
})

setTimeout(() => {
  console.log('Remove all listener')
  ee.removeAllListeners()
}, 10_000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment