Async Tiny Task Queue
npm install attqAn async tiny task queue (and related utilities), with zero dependencies. Attq
provides a data structure that executes an asynchronous callback sequentially on
a flexible list. It is generally designed to facilitate replaying ordered events
from a client to a server and comes with bells and whistles including batching,
throttling, and configurable retries.
``sh`
npm install --save attq
The AsyncTaskQueue constructor takes a task handler callback, and items are.push()
added to the queue object with the method. Additional options may be.onError()
configured with methods such as , .batchSize(), and.throttleMs().
`typescript
import { AsyncTaskQueue } from "attq";
const q = new AsyncTaskQueue
(nums: number[]) => fetch(/refine?macrodata=${nums.join(",")}),
)
// Handler will receive up to 4 items per batch.
.batchSize(4)
// Handler will be called at most once per 100 milliseconds.
.throttleMs(100);
// Add items to the queue.
for (let n = 0; n < 1000; n++) {
q.push(n);
}
await new Promise((resolve) => setTimeout(resolve, 5000));
console.log(q.size()); // Should display >=800 items remaining.
// Stop processing items if any causes an exception.
q.onError(() => {
q.drain();
});
`
Rather than build retry logic into the queue itself, Attq provides a
withRetry() higher-order function which can be wrapped around the taskwithRetry()
handler. (If desired, may be used independently of the task queue
as well!)
`typescript
import { AsyncTaskQueue, withRetry } from "attq";
let q = new AsyncTaskQueue
// Defaults to 6 attempts with binary exponential backoff.
withRetry((nums) => fetch(/refine?macrodata=${nums.join(",")})),
);
// To specify, for example, up to 3 attempts with a linear backoff:
q = new AsyncTaskQueue
withRetry(
(nums) => fetch(/refine?macrodata=${nums.join(",")}),``
{
attempts: 3,
backoffMs: (attempt) => 1000 * attempt,
},
),
);