-
Notifications
You must be signed in to change notification settings - Fork 34
Open
Description
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
sema.cancel()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
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels