@labset/task-pool-executor
TypeScript icon, indicating that this package has built-in type declarations

2.3.0 • Public • Published

task-pool-executor

Quality Gate Status Known Vulnerabilities


Provides a simple and a cli progress executor implementations to orchestrate a set of asynchronous tasks

install it

  • npm
npm install @labset/task-pool-executor --save
  • yarn
yarn add @labset/task-pool-executor

use it

  • simple task executor
import { taskPoolExecutor } from "@labset/task-pool-executor";

const executor = taskPoolExecutor<string>({ maxConcurrent: 3 });
const task = {
  title: "my-task",
  run: () => Promise.resolve("done"),
};
executor.submit(task);

await executor.close();
  • with cli progress support
import {
  CliProgressRunContext,
  cliProgressTaskPoolExecutor,
} from "@labset/task-pool-executor";

const taskPool = cliProgressTaskPoolExecutor<string>();

const delayedTask = (millis: number, title: string) => {
  const run = (ctx?: CliProgressRunContext) =>
    new Promise<string>((resolve) => {
      const interval = setInterval(() => {
        ctx?.progress.increment();
      }, 8);
      const timeout = setTimeout(() => {
        clearInterval(interval);
        resolve("done");
      }, millis);
    });
  return { title, run };
};

const tasks = new Array(10)
  .fill(0)
  .map((_v, index) => delayedTask(2000, `task ${index}`));

const internal = async () => {
  tasks.forEach((t) => taskPool.submit(t));
  await delayedTask(8000, "running").run();
  await taskPool.close();
};

internal().then(console.info).catch(console.error);

cli progress example

Readme

Keywords

Package Sidebar

Install

npm i @labset/task-pool-executor

Weekly Downloads

16

Version

2.3.0

License

Apache-2.0

Unpacked Size

33.6 kB

Total Files

11

Last publish

Collaborators

  • viqueen