Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why do listeners also have to block/wait for each other? #12

Open
ak99372 opened this issue Aug 23, 2021 · 1 comment
Open

Why do listeners also have to block/wait for each other? #12

ak99372 opened this issue Aug 23, 2021 · 1 comment

Comments

@ak99372
Copy link

ak99372 commented Aug 23, 2021

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();
@imcuttle
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants