Nodejs core Events system is synchronous. This means we can't await for listeners and async listeners can't be caught.
This package extends EventEmitter and enables event emmiters to await for listeners to resolve in a concise way.
npm install --save asynchronous-emitter
const AsyncEventEmitter = require('asynchronous-emitter');
const events = new AsyncEventEmitter();
// it's the same core EventEmitter apis and behavior
events.on('event', () => {
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log('event will resolve');
resolve();
}, 100);
});
});
// but it await events
async function main() {
await events.emit('event');
console.log('event awaited');
}
main();