Skip to content

Instantly share code, notes, and snippets.

@simonespa
Created June 8, 2020 11:28
Show Gist options
  • Select an option

  • Save simonespa/429d48f4d1f59260143a8e2230645813 to your computer and use it in GitHub Desktop.

Select an option

Save simonespa/429d48f4d1f59260143a8e2230645813 to your computer and use it in GitHub Desktop.
Node's Event Emitter and "bind" function
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