@sugarcoated/fondant-queue
v1.0.0
Published
Queue asynchronous logic.
Downloads
2
Readme
Method queues are a common pattern for throttling the execution of methods, so that one will only execute after the other, in a controller manner. Queue
brings that to asynchronous logic such as Promise
and async
/await
. Task the queue with methods you want to throttle in this way, and handle the Promise
usually expected like you would without.
- View the source for Queue at: src/actionables/Queue.js.
- View the test file for Queue at: tests/Actionables/testQueue.js.
- View the NPM package at: @sugarcoated/fondant-queue.
API Reference
Instance Methods
`new Queue([max])
Create a new Queue
.
max
is an optional parameter, expected as aNumber
, representing the maximum amount ofAsyncFunction
s orFunction.<Promise>
theQueue
can hold at any one time.new Queue() new Queue(20)
Upon construction, you will have access to the following properties:
pending
is the current amount of pendingPromise
s.maximum
is the maximum amount of pendingPromise
s allowed.
.task(method)
Add a method to the Queue
.
method
is expected as aFunction
that returns aPromise
. This can be anAsyncFunction
.myQueue.task(async () => {}).then().catch() myQueue.task(() => new Promise(() => {})).then().catch()
.pretask(method)
Add a method to the front of the Queue
.
method
is expected as aFunction
that returns aPromise
. This can be anAsync Function
.myQueue.pretask(async () => {}).then().catch() myQueue.pretask(() => new Promise(() => {})).then().catch()
Instance Events
Empty
is fired when theQueue
resolves the finalPreomise
.