@philiprehberger/async-queue
v0.1.5
Published
Priority queue for async tasks with concurrency control
Downloads
609
Readme
@philiprehberger/async-queue
Priority queue for async tasks with concurrency control
Installation
npm install @philiprehberger/async-queueUsage
import { createQueue } from '@philiprehberger/async-queue';
const queue = createQueue({ concurrency: 2 });
queue.add(() => fetch('/api/one'));
queue.add(() => fetch('/api/two'));
queue.add(() => fetch('/api/urgent'), { priority: 10 });
await queue.onIdle();
console.log('All tasks complete');Pause and resume
queue.pause();
queue.add(() => fetch('/api/deferred'));
queue.resume(); // queued tasks start executingAbort signal
const controller = new AbortController();
const queue = createQueue({ concurrency: 3, signal: controller.signal });
queue.add(() => longRunningTask());
controller.abort(); // clears pending tasksAPI
| Export | Description |
| --- | --- |
| createQueue(options?) | Create a new async queue instance |
| QueueOptions.concurrency | Max concurrent tasks (default: Infinity) |
| QueueOptions.signal | AbortSignal to cancel the queue |
| queue.add(fn, options?) | Add a task; returns a promise for its result |
| AddOptions.priority | Higher number = higher priority (default: 0) |
| queue.pause() | Pause task execution |
| queue.resume() | Resume task execution |
| queue.clear() | Reject and remove all pending tasks |
| queue.onIdle() | Promise that resolves when queue is idle |
| queue.onEmpty() | Promise that resolves when no pending tasks remain |
| queue.size | Number of pending tasks |
| queue.running | Number of currently running tasks |
| queue.isPaused | Whether the queue is paused |
Development
npm install
npm run build
npm testLicense
MIT
