Skip to content

Simple event emitter and listener in Node.js using callbacks

Callbacks can be used to write clean implementations of anything Promises in Node.js. This is a sample code for an event driven architecture using callbacks.

let events = require('events');

let listenerCallback = (data) => {
    console.log(`Celebrate ${data}`);

let myEmitter = new events.EventEmitter();

myEmitter.on('celebration', listenerCallback);

myEmitter.emit('celebration', 'birthday');
See also  How to make an Axios DELETE request?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.