
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
async-worker-queue
Advanced tools
A queue for async tasks that can be run in parallel with a maximum concurrency
async-worker-queue is an library written in TypeScript that provides a simple and efficient way to manage a queue of asynchronous tasks using worker threads.
It exposes a single class AsyncWorkerQueue that encapsulates the functionality of the package.
You can install async-worker-queue using npm:
npm install async-worker-queue
Alternatively, you can use yarn:
yarn add async-worker-queue
Or, if you prefer to use pnpm:
pnpm add async-worker-queue
To use AsyncWorkerQueue, first import the package:
import { AsyncWorkerQueue } from 'async-worker-queue';
The AsyncWorkerQueue class represents a queue of asynchronous tasks that are processed by worker threads. It provides methods to enqueue tasks and configure the behavior of the queue.
constructor(
private _createWorker: (i: number) => Promise<Execute<T, R>>,
public concurrency: number,
private _options: {
removeWorkerOnError?: boolean;
recreateWorkerOnError?: boolean;
} = {}
)
Creates an instance of AsyncWorkerQueue with the following parameters:
_createWorker (required): A function that creates and returns a worker thread. It takes an index parameter i and should return a Promise that resolves to a function Execute<T, R>. This function represents the task to be executed by the worker thread.concurrency (required): The maximum number of tasks that can be executed concurrently. This value determines the number of worker threads that will be created._options (optional): An object that allows you to configure additional options:
removeWorkerOnError (optional): A boolean value indicating whether a worker thread should be removed from the queue if it encounters an error while executing a task. Default value is false.recreateWorkerOnError (optional): A boolean value indicating whether a worker thread should be recreated after encountering an error. Default value is false.enqueue(payload: T)enqueue(payload: T): Promise<R>
Enqueues a task represented by payload to be processed by the worker threads. The method returns a Promise that resolves to the result R of the executed task.
initialise()async initialise(): Promise<void>
Initializes the worker queue by creating the necessary worker threads based on the concurrency value.
This method is optional and is called automatically when the first task is enqueued.
However, if creating a worker takes up some time,
it is recommended to call this method explicitly before enqueuing tasks to avoid any delays.
Here's a basic example that demonstrates the usage of AsyncWorkerQueue:
import { AsyncWorkerQueue } from 'async-worker-queue';
// Function that creates a worker thread
const createWorker = (i: number): Promise<Execute<T, R>> => {
// Create and return a worker thread
// ...
};
// Create an instance of AsyncWorkerQueue
const workerQueue = new AsyncWorkerQueue(createWorker, 3);
// (Optional) Initialize the worker queue
await workerQueue.initialise();
// Enqueue tasks
const result1 = await workerQueue.enqueue(payload1);
const result2 = await workerQueue.enqueue(payload2);
console.log(result1, result2);
In this example, we create an instance of AsyncWorkerQueue with a concurrency of 3,
meaning that up to 3 tasks can be executed concurrently by the worker threads.
We then initialize the queue, enqueue tasks, and await their completion, printing the results.
Contributions are welcome! If you find a bug or have a suggestion for improvement, please open a PR.
FAQs
A queue for async tasks that can be run in parallel with a maximum concurrency
The npm package async-worker-queue receives a total of 57 weekly downloads. As such, async-worker-queue popularity was classified as not popular.
We found that async-worker-queue demonstrated a not healthy version release cadence and project activity because the last version was released 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
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

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.