I find I have the need to do a series of concurrent tasks, with a max concurrency, but also with a max total run time. After this time, whatever hasn't finished or started can be ignored.
I propose to add a new API
Which rejects all pending .acquire() calls. This way I can do the following
const timeout = setTimeout(() => sema.cancel(), 5000);
await Promise.all(jobs.map(async job => {
try {
sema.acquire();
} catch (err) {
if (err.code === 'CANCELLED') {
return null
}
throw err
}
try {
await doWork(job);
} finally {
sema.release();
}
}));
clearTimeout(timeout);
If accepted I can make a PR
I find I have the need to do a series of concurrent tasks, with a max concurrency, but also with a max total run time. After this time, whatever hasn't finished or started can be ignored.
I propose to add a new API
Which rejects all pending
.acquire()calls. This way I can do the followingIf accepted I can make a PR