Installation
$ npm install queue-promise
Usage
import Queue from "queue-promise";
const queue = new Queue({
concurrent: 1,
interval: 2000
});
queue.on("resolve", data => console.log(data));
queue.on("reject", error => console.error(error));
queue.push(asyncTaskA);
queue.push(asyncTaskB);
queue.push(asyncTaskC);
queue.push(asyncTaskD);
queue.start();
API
new Queue(options)
Create a new Queue
instance.
Option | Default | Description |
---|
concurrent | 5 | How many tasks can be handled at the same time |
interval | 500 | How often should new tasks be handled (in ms) |
public .add(task)
/.push(task)
Puts a new task on the stack. Throws an error if the provided task
is not a valid function.
public .start()
Starts the queue. Queue
will use global Promises.
public .started
Whether the queue has been started or not.
public .stop()
Stops the queue.
public .on(event, callback)
Sets a callback
for an event
. You can set callback for those events: start
, stop
, tick
, resolve
, reject
, end
.
private .next()
Goes to the next request and stops the loop if there is no requests left.
private .remove(key)
/.pop(key)
/.shift(key)
Removes a task from the queue.
Tests
$ npm test