Options
All
  • Public
  • Public/Protected
  • All
Menu

A promise queue.

example
import assert from "assert"
import { PromiseQueue } from "@marcelkloubert/promises"

// create and start the queue
const queue = new PromiseQueue({
"autoStart": true,
"concurrency": 10 // maximum 10 actions at the same time
})

const promises: Promise<any>[] = []
let counter = 0

// lets create 100 actions and
// add them to queue
const actionCount = 100
for (let i = 0; i < actionCount; i++) {
promises.push(
queue.enqueue(async () => {
// do some (long) work here ...

++counter
})
)
}

// wait until all actions have been executed
await Promise.all(promises)

// stop the queue
queue.stop()

// counter should now be the number of
// enqueued actions
assert.strictEqual(counter, promises.length)

Hierarchy

  • PromiseQueue

Index

Constructors

Properties

activeItems: PromiseQueueItem[] = []
concurrency: number
itemInterval: number
nextItemsInterval: number
pendingItems: PromiseQueueItem[] = []
timer: null | Timeout = null
actionInterval: number = 100

A default value for a 'PromiseQueueOptions.actionInterval' prop.

autoStart: boolean = true

A default value for a 'PromiseQueueOptions.autoStart' prop.

concurrency: number = 1

A default value for a 'PromiseQueueOptions.concurrency' prop.

nextActionsInterval: number = 100

A default value for a 'PromiseQueueOptions.nextActionsInterval' prop.

Accessors

  • get activeLength(): number
  • get isActive(): boolean
  • Gets if queue is active or not.

    example
    import assert from "assert"
    import { PromiseQueue } from "@marcelkloubert/promises"

    const queue = new PromiseQueue()
    assert.strictEqual(queue.isActive, true)

    queue.stop()
    assert.strictEqual(queue.isActive, false)

    Returns boolean

  • get isEmpty(): boolean
  • Returns if the queue is empty or not.

    example
    import assert from "assert"
    import { PromiseQueue } from "@marcelkloubert/promises"

    const queue = new PromiseQueue()
    assert.strictEqual(queue.isEmpty, true)

    for (let i = 1; i <= 10; i++) {
    queue.enqueue(async () => {
    // do some (long) work here ...
    })

    assert.strictEqual(queue.isEmpty, false)
    }

    Returns boolean

  • get length(): number
  • Returns the number of all items of the queue.

    example
    import assert from "assert"
    import { PromiseQueue } from "@marcelkloubert/promises"

    const queue = new PromiseQueue()
    assert.strictEqual(queue.length, 0)

    for (let i = 1; i <= 10; i++) {
    queue.enqueue(async () => {
    // do some (long) work here ...
    })

    assert.strictEqual(queue.length, i)
    }

    Returns number

  • get pendingLength(): number

Methods

  • clear(): void
  • Clears the queue.

    example
    import assert from "assert"
    import { PromiseQueue } from "@marcelkloubert/promises"

    const queue = new PromiseQueue()
    assert.strictEqual(queue.isEmpty, true)

    for (let i = 1; i <= 10; i++) {
    queue.enqueue(async () => {
    // do some (long) work here ...
    })

    assert.strictEqual(queue.isEmpty, false)
    }

    queue.clear()
    assert.strictEqual(queue.isEmpty, false)

    Returns void

  • Enqueues a new action.

    example
    import assert from "assert"
    import { PromiseQueue } from "@marcelkloubert/promises"

    const queue = new PromiseQueue()
    assert.strictEqual(queue.length, 0)

    for (let i = 1; i <= 10; i++) {
    queue.enqueue(async () => {
    // do some (long) work here ...
    })

    assert.strictEqual(queue.length, i)
    }

    Type parameters

    • T = any

    Parameters

    Returns Promise<T>

    The promise with the result of the action.

  • getAllItems(): PromiseQueueItem[]
  • isInQueue(item: PromiseQueueItem): boolean
  • Checks if an item is part of this queue.

    Parameters

    • item: PromiseQueueItem

      The item to check.

    Returns boolean

    Is part of this queue or not.

  • start(): void
  • Starts the queue.

    example
    import assert from "assert"
    import { PromiseQueue } from "@marcelkloubert/promises"

    // create, but do not start the queue
    const queue = new PromiseQueue({
    "autoStart": false,
    })

    // do some preparations ...

    // now start the queue
    queue.start()

    // ...

    Returns void

  • stop(): void
  • Stops the queue.

    example
    import assert from "assert"
    import { PromiseQueue } from "@marcelkloubert/promises"

    // create and start the queue
    const queue = new PromiseQueue()

    // work with the queue ...

    // stop the queue
    queue.stop()

    Returns void

Generated using TypeDoc