Created
June 8, 2020 11:28
-
-
Save simonespa/429d48f4d1f59260143a8e2230645813 to your computer and use it in GitHub Desktop.
Node's Event Emitter and "bind" function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const { EventEmitter } = require('events'); | |
| const em = new EventEmitter(); | |
| function eventListener(response) { | |
| console.log(`Response Status: ${response.status}`); | |
| } | |
| function middleware(response) { | |
| em.on('finish', eventListener.bind(null, response)); | |
| console.log('next()'); | |
| } | |
| const response = { status: '200' }; | |
| middleware(response); | |
| setTimeout(() => { | |
| em.emit('finish'); | |
| }, 1000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment