1.0.0 • Published 6 years ago

@sugarcoated/fondant-queue v1.0.0

Weekly downloads
1
License
MIT
Repository
github
Last release
6 years ago

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.

API Reference

Instance Methods

`new Queue(max)

Create a new Queue.

  • max is an optional parameter, expected as a Number, representing the maximum amount of AsyncFunctions or Function.<Promise> the Queue 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 pending Promises.
  • maximum is the maximum amount of pending Promises allowed.

.task(method)

Add a method to the Queue.

  • methodis expected as a Function that returns a Promise. This can be an AsyncFunction.

    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 a Function that returns a Promise. This can be an Async Function.

    myQueue.pretask(async () => {}).then().catch() myQueue.pretask(() => new Promise(() => {})).then().catch()

Instance Events

  • Empty is fired when the Queue resolves the final Preomise.