Members
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().
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.
stop()
Waits for running tasks to complete. Prevents additional calls to push().