@ando_ghevian/promise-queue
TypeScript icon, indicating that this package has built-in type declarations

1.0.3 • Public • Published

PromiseQueue

This is simple class to queue promises (thenables) with maxQueueCapacity and timer.

Usage

Simply create PromiseQueue instance with maxLength, and initial timer and start to add promisies to it.

const PromiseQueue = require('promise-queue')

const queue = new PromiseQueue(2, 2000)
queue.addPromise(Promise.resolve(10), async (promise, queueMeta) => {
    console.log(queueMeta)
    const result = await promise // Here Promise is already out of pending state.
})
queue.addPromise(Promise.reject('REJECT'))
queue.addPromise(Promise.reject('REJECT2'))
queue.addPromise(Promise.reject('REJECT3'))
queue.addPromise(Promise.reject('REJECT4'))
queue.addPromise(Promise.reject('REJECT5'))

Here promises will be added to queue when possible, i.e. if queue capacity is not reached maxLength limit, promise will be added to queue, and awaited.

If it is reached limit, queue will idle for timer time(2000ms in this case), and there after check again if there is any free capacity to add promise. timer default value is 25ms.

You can also await queue.addPromise() call, to be aware when current promise already added to queue and awaited.

await queue.addPromise(Promise.resolve('already added to queue'))
// ...In here promise already added to queue and awaited,

Buy second argument you can give callback as you see, that will be called, when promise rejected or resolved and poped out from the queue.

There you will get as a first argument same promise, that is allready in ReadyState, and QueueMeta information with second argument, which presents state of a queue at a time callback called (when promise resolved or rejected).

Currently you can change timer value of queue instance, and when next time timer starts, it will use new value. See when timer starts above.

Readme

Keywords

Package Sidebar

Install

npm i @ando_ghevian/promise-queue

Weekly Downloads

1

Version

1.0.3

License

ISC

Unpacked Size

18.5 kB

Total Files

12

Last publish

Collaborators

  • ando_ghevian