sindresorhus / p-queue

Promise queue with concurrency control
MIT License
3.39k stars 182 forks source link

Testing `complete` event when its the only non-respondent event. #169

Open patrickbjohnson opened 1 year ago

patrickbjohnson commented 1 year ago

Current Stack

  1. Vercel
  2. Nuxt
  3. Sanity

Context

We're using p-queue within our Sanity Studio dashboard to run some batched actions. When setting this up all events run except for completed. Which is really a head scratcher. I've created a reduced case in Replit and it works there, great! However, in my current setup all other methods seem to work except for complete so trying to debug a bit futher.

My code setup below:

const stripeQueue = new PQueue({
  concurrency: 2,
})

stripeQueue.on('completed', result => {
    console.log('completd: ', result);
});

stripeQueue.on('idle', (data) => {
  console.log('queue id idle: ', data);
});

stripeQueue.on('next', () => {
    console.log(`Next:: Task is completed.  Size: ${stripeQueue.size}  Pending: ${stripeQueue.pending}`);
});

stripeQueue.on('error', (error) => {
    console.log(`Error:: `, error);
});

const addItemsToQueue = () => {
  console.log('before stripeQueue add')
  stripeQueue.add(() => Promise.resolve('hello, world 1!'));
  console.log('after 1')
  stripeQueue.add(() => Promise.resolve('hello, world 2!'));
  console.log('after 2')
}

function TestComponent () {
  return(
    <div
      header="Members"
    >
      <button onClick={() => addItemsToQueue()}>Click me</button>  
    </div>
  )
}

What's crazy, even in that small test case in my environment this is the result:

before stripeQueue add
index.js:151 after 1
index.js:153 after 2

index.js:133 Next:: Task is completed.  Size: 2  Pending: 2
index.js:133 Next:: Task is completed.  Size: 1  Pending: 2
index.js:133 Next:: Task is completed.  Size: 0  Pending: 2
index.js:133 Next:: Task is completed.  Size: 0  Pending: 1
index.js:129 queue id idle:  undefined
index.js:133 Next:: Task is completed.  Size: 0  Pending: 0

I do realize you request a MVP use case. I'm happy to attempt to make one past the basic Replit however it may be challenging given the use case is within a CMS that is using React on the front-end and sending data back to the server.

patrickbjohnson commented 1 year ago

This turned out to be a "personal problem". This code does not work in the browser but does in node.