I'm not sure if this is the right place to submit an idea, but it would be interesting to have a function p
that allows executing a specified number of promises concurrently while a condition is true
.
In my specific case, I have a list with 200 items and need to check if at least 5 of them have a certain "quality". Since it is a time-consuming process and the outcome is quite random, I activate 10 concurrent executions and keep performing an items.shift()
operation to test each item.
Once I reach 5 successful elements, I pause the processing. However, when the number of elements drops below 5 again, I need to resume searching for new elements. In my particular case, it's not a problem if I find more than 5 elements, as some of the concurrent executions may still be pending even after pausing.
Here's a rough outline of the idea:
const p = pFunctionHere({
concurrency: 10,
afterPausedInterval: 100, // When the condition is reached the first time (returning `false`), then every 100ms it will be executed again to see if it is necessary to restart the concurrencies.
});
const tester = async () => {
// Get an item from the list.
const item = items.shift();
if (await isValid(item)) { // Time-consuming process.
const testItem = addItem(item); // addedItems.push(...)
testItem.test(); // Test the item every minute.
}
}
const condition = () => {
return items.length > 0 && addedItems.length < 5;
}
p.run(tester, condition);
This feature would allow executing tester function concurrently up to the specified concurrency limit (10 in this case) while the condition condition
is true
. Once the condition is no longer met, the execution would pause until the condition becomes true
again.
It would be great to have this functionality as it provides a convenient way to handle concurrent promise execution with a condition.
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too