English | 简体中文
@antmind/task-pool is a simple Node.js functional tasks pool implementation, supported both synchronous and asynchronous functions.
-
Using NPM:
npm install --save @antmind/task-pool
-
Using Yarn:
yarn add @antmind/task-pool
-
Import
TaskPoolandTaskfrom@antmind/task-pool. -
Create a new task pool instance, and you can set concurrency limit if you need.
-
Create tasks instance and add them into task pool.
-
Call
exec()method to execute functions.
import { Task, TaskPool } from '@antmind/task-pool';
const pool = new TaskPool();
for (let i = 5; i > 0; i -= 1) {
const task = new Task((val: any) => val, i);
pool.addTask(task);
}
pool.exec().then((data: any) => console.log(data));
// [ 5, 4, 3, 2, 1 ]You can limit the task concurrency number by concurrency option, and this value must equal or greater than 0.
import { Task, TaskPool } from '@antmind/task-pool';
const pool = new TaskPool({ concurrency: 3 });
for (let i = 5; i > 0; i -= 1) {
pool.addTask(
new Task(
(val: any) => new Promise((resolve: Function) => {
setTimeout(
() => {
console.log(`num: ${val}`);
resolve(val);
},
val * 100,
);
}),
i,
),
);
}
pool.exec().then((data) => console.log(data));
// num: 3
// num: 4
// num: 5
// num: 1
// num: 2
// [ 5, 4, 3, 2, 1 ]You can set concurrency option as 0 to enable unlimited concurrency mode, it's similar with Promise.all.
import { Task, TaskPool } from '@antmind/task-pool';
const pool = new TaskPool({ concurrency: 0 });
for (let i = 5; i > 0; i -= 1) {
pool.addTask(
new Task(
(val: any) => new Promise((resolve: Function) => {
setTimeout(
() => {
console.log(`num: ${val}`);
resolve(val);
},
val * 100,
);
}),
i,
),
);
}
pool.exec().then((data) => console.log(data));
// num: 1
// num: 2
// num: 3
// num: 4
// num: 5
// [ 5, 4, 3, 2, 1 ]-
concurrency: The tasks maximum concurrency limit number, it should be a integer number greater or equals to0, and the default value is30. Set this option value to0to enable unlimited concurrency mode. -
throwsError: Throw error when some task failed if this option set totrue, and do not throw error if set tofalse(you can get errors bygetErrors()method). The default value istrue.
-
constructor() -
constructor(options: TaskPoolOptions) -
constructor(task: Task | Task[], options?: TaskPoolOptions)
-
exec(): Promise<any[]>Execute all tasks in the pool, and it'll return a result array after executing.
-
addTask(task: Task): numberAdd a task into task pool, and it'll return the task id.
-
addTasks(tasks: Task[]): number[]Add a tasks array into task pool, and it'll return the tasks' id.
-
setConcurrency(concurrency: number): voidSet concurrency limits.
-
getErrors(): Array<Error | undefined>Get errors of last execution, and the index of error is same as task index.
-
getTask(id: number): Task | nullGet task by id.
constructor(func: Function, ...args: any[])
-
exec(): anyExecute this task.
-
setArgs(...args: any[]): voidSet function arguments.
This project has been published under MIT license, you can get more detail in LICENSE file.