Skip to content

createKeyedTaskQueue ​

Creates an in-memory queue that runs tasks with the same key serially in submission order. Tasks with different keys can run concurrently.

Usage ​

ts
import { createKeyedTaskQueue } from 'rattail'

const queue = createKeyedTaskQueue<string>()

const first = queue.run('conversation-a', async () => 'first result')
const second = queue.run('conversation-a', async () => 'second result')
const independent = queue.run('conversation-b', () => 42)

queue.isBusy('conversation-a') // true
await Promise.all([first, second, independent])
queue.isBusy('conversation-a') // false

Methods ​

MethodReturnDescription
run(key, task)Promise<Awaited<Result>>Enqueue a synchronous or asynchronous task and return its result
isBusy(key)booleanWhether the key has a running or queued task

Notes ​

  • The key type defaults to string; object keys compare by reference.
  • A failed task rejects its own promise without blocking later tasks.
  • Asynchronous tasks must return a promise covering their entire operation.
  • Reuse the same queue instance for tasks that need coordination. Within a task, do not await another task with the same key on that queue: it will deadlock.