Skip to content

gustawdaniel/async-queue

Repository files navigation

Node.js Package

Async Queue

Super simple below 100 lines queue implementation build with Promise.race and AsyncIterator.

Features:

  • you can quite it by stop loosing all unprocessed items that it useful in tests
  • you do not have to connect with redis or open any port, everything works in memory
  • define simple processing function and forget about it, now you can simply push items to process

Installation

npm i @gustawdaniel/queue-async

Usage

import {AsyncQueue, Queue} from '@gustawdaniel/queue-async';

let sum = 0
const queue: Queue<number> = new AsyncQueue<number>(async (item) => {
    sum += item
})
for (const i of new Array(6).fill(1)) {
    queue.push(i)
}
queue.start()
await sleep(1)
expect(sum).toEqual(6)

Interface

export interface Queue<T> {
    state: 'open' | 'closed'

    push(item: T): void

    pull(): Promise<T>

    start(): void

    stop(): void

    [Symbol.asyncIterator](): AsyncIterator<T>
}

About

Simple minimalistic Queue build on Promise.race and AsyncIterator

Resources

Stars

Watchers

Forks

Packages

No packages published