@shogo82148/limit-concurrency
TypeScript icon, indicating that this package has built-in type declarations

0.1.6 • Public • Published

limit_concurrency

It runs the tasks in parallel but ensures that at any given time, no more than the specified limit of tasks are being executed concurrently. Once a task completes, another task (if any remain) is started, maintaining the concurrency limit until all tasks have been executed.

import { limitConcurrency } from "./limit-concurrency.ts";

function* taskGenerator(): Generator<() => Promise<void>> {
    for (let i = 0; i < 10; i++) {
        yield async () => {
            // Simulate an asynchronous task
            console.log(`Task ${i} started`);
            await new Promise((resolve) => setTimeout(resolve, 1000));
            console.log(`Task ${i} finished`);
        };
    }
}

const iterator = taskGenerator();
limitConcurrency(iterator, 2); // Limit concurrency to 2

Readme

Keywords

none

Package Sidebar

Install

npm i @shogo82148/limit-concurrency

Weekly Downloads

28

Version

0.1.6

License

MIT

Unpacked Size

39.3 kB

Total Files

85

Last publish

Collaborators

  • shogo82148