Concurrent Execution
Set the concurrency option to a value greater than 1 to execute multiple tasks in parallel.
Task Priority
When adding a task via .add(task, priority), you can specify a priority level. Lower numerical values indicate higher priority. The queue automatically maintains the execution order based on these values.
Dynamic Concurrency Adjustment
You can change the number of concurrent tasks at runtime using .setConcurrency(n) and check the current value with .getConcurrency().
// Concurrent execution example
const concurrentQueue = new TaskQueue({ concurrency: 3 });
const tasks = Array.from({ length: 5 }, (_, i) =>
concurrentQueue.add(async () => {
return `Task ${i + 1} complete`;
})
);
const results = await Promise.all(tasks);
// Priority example
const lowPriorityTask = queue.add(async () => 'low', 10);
const highPriorityTask = queue.add(async () => 'high', 1);
// Dynamic adjustment
queue.setConcurrency(4);
console.log(queue.getConcurrency()); // 4