All Projects → Bartozzz → queue-promise

Bartozzz / queue-promise

Licence: MIT License
A simple, dependency-free library for concurrent promise-based queues. Comes with with concurrency and timeout control.

Programming Languages

javascript
184084 projects - #8 most used programming language
typescript
32286 projects

Projects that are alternatives of or similar to queue-promise

P Queue
Promise queue with concurrency control
Stars: ✭ 1,863 (+3226.79%)
Mutual labels:  queue, promise, promise-queue
Promise Queue Plus
Promise-based queue. Support timeout, retry and so on.
Stars: ✭ 113 (+101.79%)
Mutual labels:  queue, promise
best-queue
Queue in runtime based promise
Stars: ✭ 26 (-53.57%)
Mutual labels:  queue, promise
Promise Fun
Promise packages, patterns, chat, and tutorials
Stars: ✭ 3,779 (+6648.21%)
Mutual labels:  promise, promise-queue
Promise Queue
Promise-based queue
Stars: ✭ 210 (+275%)
Mutual labels:  queue, promise
Wx Promise Request
解决微信小程序 wx.request 请求的并发数限制、不支持异步问题
Stars: ✭ 226 (+303.57%)
Mutual labels:  queue, promise
Task Easy
A simple, customizable, and lightweight priority queue for promises.
Stars: ✭ 244 (+335.71%)
Mutual labels:  queue, promise
wtsqs
Simplified Node AWS SQS Worker Wrapper
Stars: ✭ 18 (-67.86%)
Mutual labels:  queue, promise
easy-promise-queue
An easy JavaScript Promise queue which is automatically executed, concurrency controlled and suspendable.
Stars: ✭ 31 (-44.64%)
Mutual labels:  queue, promise
openmessaging.github.io
OpenMessaging homepage
Stars: ✭ 12 (-78.57%)
Mutual labels:  queue
jrsmq
A lightweight message queue for Java that requires no dedicated queue server. Just a Redis server.
Stars: ✭ 28 (-50%)
Mutual labels:  queue
promise
A step by step implementation practice of Promise class
Stars: ✭ 31 (-44.64%)
Mutual labels:  promise
popyt
A very easy to use Youtube Data v3 API wrapper.
Stars: ✭ 42 (-25%)
Mutual labels:  promise
Actuate
One line easy actuation of CSS animation sequences
Stars: ✭ 42 (-25%)
Mutual labels:  promise
swear
🙏 Flexible promise handling with Javascript
Stars: ✭ 56 (+0%)
Mutual labels:  promise
ArduinoQueue
A lightweight linked list type queue implementation, meant for microcontrollers.
Stars: ✭ 29 (-48.21%)
Mutual labels:  queue
Eventually
A Swift Future/Promise library that can be used to model and transform asynchronous results
Stars: ✭ 19 (-66.07%)
Mutual labels:  promise
libmcu
A toolkit for firmware development
Stars: ✭ 33 (-41.07%)
Mutual labels:  queue
DSA
Data Structures and Algorithms
Stars: ✭ 13 (-76.79%)
Mutual labels:  queue
turnio
🤖 Bot to manage a queue of slack users in a channel
Stars: ✭ 11 (-80.36%)
Mutual labels:  queue

queue-promise

Default CI/CD Known Vulnerabilities npm package size npm version npm dependency Status npm downloads

queue-promise is a small, dependency-free library for promise-based queues. It will execute enqueued tasks concurrently at a given speed. When a task is being resolved or rejected, an event is emitted.

Installation

$ npm install queue-promise

Usage

With automatic start

import Queue from "queue-promise";

const queue = new Queue({
  concurrent: 1,
  interval: 2000
});

queue.on("start", () => /* … */);
queue.on("stop", () => /* … */);
queue.on("end", () => /* … */);

queue.on("resolve", data => console.log(data));
queue.on("reject", error => console.error(error));

queue.enqueue(asyncTaskA); // resolved/rejected after 0ms
queue.enqueue(asyncTaskB); // resolved/rejected after 2000ms
queue.enqueue(asyncTaskC); // resolved/rejected after 4000ms

Without automatic start

import Queue from "queue-promise";

const queue = new Queue({
  concurrent: 1,
  interval: 2000,
  start: false,
});

queue.enqueue(asyncTaskA);
queue.enqueue(asyncTaskB);
queue.enqueue(asyncTaskC);

while (queue.shouldRun) {
  // 1st iteration after 2000ms
  // 2nd iteration after 4000ms
  // 3rd iteration after 6000ms
  const data = await queue.dequeue();
}

API

new Queue(options)

Create a new Queue instance.

Option Default Description
concurrent 5 How many tasks should be executed in parallel
interval 500 How often should new tasks be executed (in ms)
start true Whether it should automatically execute new tasks as soon as they are added

public .enqueue(tasks)/.add(tasks)

Adds a new task to the queue. A task should be an async function (ES2017) or return a Promise. Throws an error if the provided task is not a valid function.

Example:

async function getRepos(user) {
  return await github.getRepos(user);
}

queue.enqueue(() => {
  return getRepos("userA");
});

queue.enqueue(async () => {
  await getRepos("userB");
});

// …equivalent to:
queue.enqueue([() => getRepos("userA"), () => getRepos("userB")]);

public .dequeue()

Executes n concurrent (based od options.concurrent) promises from the queue. Uses global Promises. Is called automatically if options.start is set to true. Emits resolve and reject events.

Example:

queue.enqueue(() => getRepos("userA"));
queue.enqueue(() => getRepos("userB"));

// If "concurrent" is set to 1, only one promise is executed on dequeue:
const userA = await queue.dequeue();
const userB = await queue.dequeue();

// If "concurrent" is set to 2, two promises are executed concurrently:
const [userA, userB] = await queue.dequeue();

Note:

.dequeue() function throttles (is executed at most once per every options.interval milliseconds).

public .on(event, callback)

Sets a callback for an event. You can set callback for those events: start, stop, resolve, reject, dequeue, end.

Example:

queue.on("dequeue", () => );
queue.on("resolve", data => );
queue.on("reject", error => );
queue.on("start", () => );
queue.on("stop", () => );
queue.on("end", () => );

Note:

dequeue, resolve and reject events are emitted per task. This means that even if concurrent is set to 2, 2 events will be emitted.

public .start()

Starts the queue – it will automatically dequeue tasks periodically. Emits start event.

queue.enqueue(() => getRepos("userA"));
queue.enqueue(() => getRepos("userB"));
queue.enqueue(() => getRepos("userC"));
queue.enqueue(() => getRepos("userD"));
queue.start();

// No need to call `dequeue` – you can just listen for events:
queue.on("resolve", data => );
queue.on("reject", error => );

public .stop()

Forces the queue to stop. New tasks will not be executed automatically even if options.start was set to true. Emits stop event.

public .clear()

Removes all tasks from the queue.

public .state

  • 0: Idle state;
  • 1: Running state;
  • 2: Stopped state;

public .size

Size of the queue.

public .isEmpty

Whether the queue is empty, i.e. there's no tasks.

public .shouldRun

Checks whether the queue is not empty and not stopped.

Tests

$ npm test

Contributing

Development

We have prepared multiple commands to help you develop queue-promise on your own. You will need a local copy of Node.js installed on your machine. Then, install project dependencies using the following command:

$ npm install

Usage

$ npm run <command>

List of commands

Command Description
test Run all test:* commands described below.
test:flow Test Flow types.
test:typescript Test TypeScript types.
test:unit Run unit tests.
test:lint Run linter tests.
defs:flow Build Flow type definitions.
defs:typescript Build TypeScript type definitions.
clean Clean dist directory.
build Build package and generate type definitions.
watch Build package in watch mode.
release Bump package version and generate a CHANGELOG.md file.

License

queue-promise was created and developed by Bartosz Łaniewski. The full list of contributors can be found here. The package is MIT licensed.

Bug reporting

Github Open Issues Github Closed Issues Github Pull Requests

We want contributing to queue-promise to be fun, enjoyable, and educational for anyone, and everyone. Changes and improvements are more than welcome! Feel free to fork and open a pull request. We use Conventional Commits specification for commit messages. If you have found any issues, please report them here - they are being tracked on GitHub Issues.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].