Socket
Socket
Sign inDemoInstall

queue-promise

Package Overview
Dependencies
0
Maintainers
1
Versions
19
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    queue-promise

A simple, dependency-free library for concurrent promise-based queues


Version published
Weekly downloads
13K
increased by6.29%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

queue-promise

Greenkeeper badge Build Status npm version npm downloads

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

Installation

$ npm install queue-promise

Usage

import Queue from "queue-promise";

const queue = new Queue({
  concurrent: 1,  // resolve 1 task at a time
  interval: 2000, // resolve new tasks each 2000ms,
  start: true,    // automatically resolve new tasks when they are added
});

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

queue.enqueue(asyncTaskA); // resolved/rejected after 0s
queue.enqueue(asyncTaskB); // resolved/rejected after 2s
queue.enqueue(asyncTaskC); // resolved/rejected after 4s
queue.enqueue(asyncTaskD); // resolved/rejected after 6s

API

new Queue(options)

Create a new Queue instance.

OptionDefaultDescription
concurrent5How many tasks can be handled at the same time
interval500How often should new tasks be handled (in ms)
starttrueWhether we should automatically resolve new tasks as soon as they are added
public .enqueue(task)/.add(task)

Puts a new task on the stack. Tasks should be an async function 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(getRepos("userA"));
queue.enqueue(getRepos("userB"));

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

Resolves n concurrent promises from the queue. Uses global Promises.

Example:

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

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

// If "concurrent" is set to 2, two promises are resolved concurrently:
const users = await queue.dequeue();
public .on(event, callback)

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

Example:

queue.enqueue([…]);

queue.on("resolve", output => …);
queue.on("reject", output => …);
queue.on("end", () => …);
public .start()

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

public .stop()

Stops the queue. Emits stop event.

public .clear()

Removes all tasks from the queue.

public .started

Whether the queue has been started or not.

public .isEmpty

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

Tests

$ npm test

Keywords

FAQs

Last updated on 06 Apr 2018

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc