MemoryQueue is a local in-memory queue that allows you to manage tasks with a maximum number of concurrent executions. It supports pausing and resuming the queue, and provides a way to handle task errors.
To install the necessary dependencies, run:
npm install
To install the MemoryQueue
package from the GitHub repository, run:
npm install git+https://github.com/kzamurnyak/memory-queue.git
First, import the MemoryQueue
class:
import {MemoryQueue} from './MemoryQueue';
Importing MemoryQueue as npm module:
import {MemoryQueue} from 'memory-queue';
Create a new instance of MemoryQueue
with a worker function and a maximum number of concurrent tasks:
const worker = async (value) => {
// Your async task logic here
};
const queue = new MemoryQueue(worker, 2);
Use the push
method to add tasks to the queue. You can also provide an optional callback that will be called after the
task is completed:
queue.push(1, () => {
console.log('Task 1 completed');
});
queue.push(2);
queue.push(3);
Use the waitForAll
method to wait until all tasks in the queue are processed:
await queue.waitForAll();
console.log('All tasks completed');
You can pause the queue to stop processing new tasks and resume it later:
queue.pause();
queue.push(4); // This task will not start until the queue is resumed
queue.resume();
You can subscribe to task errors using the onTaskError
method:
queue.onTaskError((error) => {
console.error('Task error:', error);
});
To run the tests, use the following command:
npx jest
This project is licensed under the MIT License.