TaskQueue

TaskQueue

A queue that enforces a maximum number of simultaneously executing tasks

Constructor

new TaskQueue(options)

Constructor. There is no need to call start() after creating a new object.

Source:
Parameters:
Name Type Description
options object
Name Type Description
size number

The maximum number of functions that can execute at a time

Members

full

Is the queue full?

Source:

Methods

push(task) → {Promise}

Starts a task. If the queue's maximum size has been reached, this method waits for a task to finish before invoking task().

Source:
Parameters:
Name Type Description
task function

A function to call. It can return a Promise, throw an exception, or return a value.

Throws:

If stop() has been called

Type
StoppingError
Returns:
Type:
Promise

Does not reject. Resolves to an object with the property 'promise' containing either the Promise returned by task or a new Promise that resolves to the value returned by task or rejects using the exception thrown by it. Therefore, it is not only possible to wait for the task to start, it is also possible to wait for it to finish.

For example: // Wait for an open slot in the queue const ret = await queue.push(()=>new Promise(resolve=>setTimeout(()=>resolve('Hello'), 5000))); // Wait for 5 seconds and output Hello console.log(await ret.promise);

start()

Undo method for stop(). There is no need to invoke start() after creating a new object.

Source:

stop()

Waits for running tasks to complete. Prevents additional calls to push().

Source:

wait() → {Promise}

Waits for running tasks to complete. Callers are not prevented callers from calling push(); thus there is no guarantee that when the returned Promise resolves, the queue will have an available slot.

Source:
Returns:
Type:
Promise