A class-based task runner with priority support and concurrency control.
Constructor
const queue = new Queue ( options ?: QueueOptions );
Parameters
Queue configuration options. Maximum number of tasks to run concurrently.
Whether to automatically start processing tasks. Set to false to manually control with pause() and resume().
Methods
add
Adds a task to the queue. Supports optional priority (higher executes sooner).
add < T >( fn : () => PromiseLike < T > | T , options ?: AddOptions ): Promise < T >
Parameters
fn
() => PromiseLike<T> | T
required
The task function to execute.
Task options. Task priority. Higher values execute sooner.
Optional AbortSignal to cancel the task.
Returns
A promise that resolves with the result of the task.
addAll
Adds multiple tasks to the queue.
addAll < T >( fns : (() => Promise < T > )[], options ?: AddOptions ): Promise < T [] >
Parameters
fns
(() => Promise<T>)[]
required
Array of task functions to execute.
Options applied to all tasks.
Returns
A promise that resolves with an array of all task results.
pause
Pauses execution. Active tasks continue, pending tasks wait.
resume
Resumes execution of pending tasks.
clear
Clears all pending tasks. Active tasks are not affected.
Properties
size
Number of items waiting to run.
pending
Number of items currently running.
onIdle
Promise that resolves when the queue is empty and all tasks have completed.
get onIdle (): Promise < void >
Examples
Basic queue
import { Queue } from '@temelj/async' ;
const queue = new Queue ({ concurrency: 2 });
const results = await Promise . all ([
queue . add (() => fetchData ( 1 )),
queue . add (() => fetchData ( 2 )),
queue . add (() => fetchData ( 3 )),
queue . add (() => fetchData ( 4 )),
]);
console . log ( results );
Priority queue
import { Queue } from '@temelj/async' ;
const queue = new Queue ({ concurrency: 1 });
queue . add (() => console . log ( 'Low priority' ), { priority: 0 });
queue . add (() => console . log ( 'High priority' ), { priority: 10 });
queue . add (() => console . log ( 'Medium priority' ), { priority: 5 });
// Output:
// High priority
// Medium priority
// Low priority
Wait for completion
import { Queue } from '@temelj/async' ;
const queue = new Queue ({ concurrency: 2 });
queue . add ( async () => {
await delay ( 100 );
console . log ( 'Task 1' );
});
queue . add ( async () => {
await delay ( 50 );
console . log ( 'Task 2' );
});
// Wait for all tasks to complete
await queue . onIdle ;
console . log ( 'All tasks done' );
Pause and resume
import { Queue } from '@temelj/async' ;
const queue = new Queue ({ concurrency: 1 });
queue . add (() => console . log ( 'Task 1' ));
queue . add (() => console . log ( 'Task 2' ));
queue . pause ();
console . log ( 'Queue paused' );
queue . add (() => console . log ( 'Task 3' ));
setTimeout (() => {
console . log ( 'Resuming queue' );
queue . resume ();
}, 1000 );
Manual start control
import { Queue } from '@temelj/async' ;
const queue = new Queue ({
concurrency: 2 ,
autoStart: false
});
// Add tasks while paused
queue . add (() => fetchData ( 1 ));
queue . add (() => fetchData ( 2 ));
queue . add (() => fetchData ( 3 ));
console . log ( ` ${ queue . size } tasks queued` );
// Start processing when ready
queue . resume ();
With abort signal
import { Queue } from '@temelj/async' ;
const queue = new Queue ({ concurrency: 2 });
const controller = new AbortController ();
const task1 = queue . add (
async () => {
await delay ( 1000 );
return 'Done' ;
},
{ signal: controller . signal }
);
// Cancel task
controller . abort ();
try {
await task1 ;
} catch ( error ) {
console . log ( 'Task cancelled' );
}
Rate-limited API calls
import { Queue } from '@temelj/async' ;
const queue = new Queue ({ concurrency: 5 });
const userIds = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ];
const users = await queue . addAll (
userIds . map ( id => async () => {
const response = await fetch ( `/api/users/ ${ id } ` );
return await response . json ();
})
);
console . log ( `Fetched ${ users . length } users` );
Clear pending tasks
import { Queue } from '@temelj/async' ;
const queue = new Queue ({ concurrency: 1 });
queue . add ( async () => {
await delay ( 100 );
console . log ( 'This will run' );
});
queue . add (() => console . log ( 'This will be cleared' ));
queue . add (() => console . log ( 'This will be cleared too' ));
queue . clear ();
await queue . onIdle ;
console . log ( 'Done' );