events

Much of the Node.js core API is built around an idiomatic asynchronous event-driven architecture in which certain kinds of objects (called "emitters") emit named events that cause Function objects ("listeners") to be called.

const EventEmitter = require('node:events');
const celebrity = new EventEmitter();

// Subscribe to celebrity for Observer 1
celebrity.on('race', (result) => {
    if (result === 'win') {
        console.log('Congratulation! You are the best!');
    }
});

// Subscribe to celebrity for Observer 2
celebrity.on('race', (result) => {
    if (result === 'win') {
        console.log('Boo I could have done better than that!');
    }
});

celebrity.emit('race', 'win');
celebrity.emit('race', 'lost');