node-await-event-emitter
node-await-event-emitter copied to clipboard
Why do listeners also have to block/wait for each other?
Having emitter wait for all subscribers/listeners is quite useful however, I don't see a reason why multiple listeners have to block each other and executed in sequence (they should run independently). The listeners should run in parallel (i.e. Promise.all(listeners)). (E.g. code below shows C being blocked until B is finished)
const AwaitEventEmitter = require('await-event-emitter').default
const emitter = new AwaitEventEmitter()
const tick = (name,args,time) =>
new Promise((resolve) => {
setTimeout(() => {
console.log(`${name} -${args}`);
resolve()
}, time || 1000)
})
emitter.on('event', async (args) => { await tick('A',args) });
emitter.on('event', async (args) => { await tick('B',args,4000) });
emitter.on('event', async (args) => { await tick('C',args) });
async function run() {
await emitter.emit('event', 'one')
await emitter.emit('event', 'two')
emitter.emitSync('event', 'three')
}
run();
Hi, @ak99372 node-await-event-emitter is just implements the series processing, If you need parallel case, Please use the package tapable which is used by webpack.