
Security News
Axios Maintainer Confirms Social Engineering Attack Behind npm Compromise
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.
parallel-task-runner
Advanced tools
A simple and flexible parallel task queue for promise-based jobs.
This package is a simple and flexible parallel task queue for promise-based jobs, written in TypeScript. It allows you to control the concurrency of your asynchronous tasks, add delays between task executions, and automatically retry failed tasks. It's lightweight and has zero dependencies 🙌.
idle, empty, and error states.npm install parallel-task-runner
First, import and create a new TaskQueue instance with your desired configuration.
import TaskQueue from 'parallel-task-runner';
const queue = new TaskQueue({
concurrency: 2, // Run a maximum of 2 tasks at once
delay: 1000, // Wait 1 second between each task
});
Then, add tasks to the queue using push() or unshift(). A task can be a function, an async function, or function that returns a promise.
queue.push(
async () => await setTimeout(1000), // Task 1
() => console.log('Task 2'), // Task 2
() => new Promise((resolve) => setTimeout(resolve, 500)), // Task 3
);
You can listen for events to know when the queue is idle, empty, or when a task has failed.
queue.on('idle', ({ pendingTasks }) => {
console.log(`The queue is now idle. Pending tasks: ${pendingTasks}`);
});
queue.on('empty', () => {
console.log('All tasks have been run.');
});
queue.on('error', (error, job) => {
console.warn(`Task failed:`, job.task);
console.error(`Error:`, error);
});
The idle and empty events also have promise-based versions.
const { pendingTasks } = await queue.waitForIdle();
console.log(`The queue is now idle. Pending tasks: ${pendingTasks}`);
await queue.waitForEmpty();
console.log('All tasks have been run.');
The queue can automatically retry failed tasks.
const queue = new TaskQueue({
retryCount: 2, // Retry failed tasks 2 times
retryDelay: 500, // Wait 500ms before retrying
});
queue.push(() => {
return new Promise((resolve, reject) => {
if (Math.random() > 0.5) {
resolve();
} else {
reject(new Error('Task failed!'));
}
});
});
new TaskQueue(config?: TaskQueueConfig)Creates a new task queue.
Configuration:
concurrency (optional, default: Infinity)
delay (optional, default: 0)
retryCount (optional, default: 3)
0 to disable.retryDelay (optional, default: 0)
retryDelay + delay.queue.push(...tasks: Runnable[]): voidSchedules new tasks by appending them to the end of the queue.
tasks: New tasks to run. A task is a function that returns a promise or void.queue.unshift(...tasks: Runnable[]): voidSchedules new tasks by inserting them at the front of the queue.
tasks: New tasks to run. A task is a function that returns a promise or void.queue.clear(): voidClears the queue, removing all pending (unstarted) tasks. Does not stop any tasks that have already started.
queue.waitForIdle(): Promise<IdleEventDetail>A promise-based version of the idle event. Returns a promise that resolves once there are no currently running tasks. However, there may still be pending tasks waiting to run. For more details, see the idle event.
queue.waitForEmpty(): Promise<void>A promise-based version of the idle event. Returns a promise that resolves once there are no currently running tasks and the queue is empty.
idle: Emitted when the queue is idle (no active tasks). However, there may still be pending tasks waiting to run.empty: Emitted when the queue is idle and there are no pending tasks left.error: Emitted when a task fails after all retry attempts.idle: (detail: IdleEventDetail)Emitted when there are no currently running tasks. However, there may still be pending tasks waiting to run. For example, with delay set to 500 ms, if two tasks are added and the first task only takes 100ms to run, then the queue will be idle for 400ms. The event provides an object with a pendingTasks property, which indicates the number of scheduled tasks in the queue.
empty: (void)Emitted when there are no currently running tasks and the queue is empty.
error: (error: unknown, job: JobInfo)Emitted when a task fails after all retry attempts have been exhausted. The event provides the error thrown by the task and a job object containing metadata about the failed task.
The job object has the following properties:
task: The original task function that failed.retriesLeft: The number of retries remaining for this task (will always be 0).delayUntil: The timestamp when the last retry was scheduled.This project is licensed under the MIT License.
FAQs
A simple and flexible parallel task queue for promise-based jobs.
We found that parallel-task-runner demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.

Security News
The Axios compromise shows how time-dependent dependency resolution makes exposure harder to detect and contain.