← All packages

@lacspace/queue

v1.0.0Core Runtime Kit0 deps

An in-memory async task queue with a concurrency limit — run up to N async tasks at once and await individual results or the whole drain. Priority ordering, pause/start, clear, onIdle/onEmpty draining, and per-task abort via AbortSignal. A rejected task never stalls the queue. Comparable to p-queue/p-limit but tiny and dependency-free. Isomorphic.

npm i @lacspace/queue

Usage

queue.ts
import { createQueue } from "@lacspace/queue";

// run at most 2 tasks at once
const queue = createQueue({ concurrency: 2 });

const results = await Promise.all(
  urls.map((u) => queue.add(() => fetch(u).then((r) => r.json()))),
);

// higher priority jumps the line; a rejected task never stalls the queue
queue.add(fetchCritical, { priority: 10 });

console.log(queue.pending, queue.size); // running, waiting
await queue.onIdle(); // resolves when fully drained

Exports 3

createQueueAbortErrorQueueClearedError

Keywords

queuetask-queuejob-queueconcurrencyconcurrency-limitp-queue-alternativep-limit-alternativeasync-queuepriority-queuezero-dependency

More in Core Runtime Kit