Implement backpressure using onSizeLessThan()
mainTo prevent the queue from growing unbounded and causing memory issues when producers are faster than consumers, use .onSizeLessThan(). This allows you to wait for the queue to have space before adding more tasks.
Note that .size counts queued items, while .pending counts running items. The total number of items in the queue is queue.size + queue.pending.
const queue = new PQueue();
// Wait for queue to have space before adding more
await queue.onSizeLessThan(100);
queue.add(() => someTask());