Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

promise-queue

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

promise-queue

Promise-based queue

  • 2.2.4
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.1M
increased by3.44%
Maintainers
1
Weekly downloads
 
Created

What is promise-queue?

The promise-queue npm package is designed to manage the execution of Promises in a queue with a configurable concurrency limit. This means that it allows for the sequential or controlled concurrent execution of asynchronous operations, which is particularly useful in scenarios where executing too many operations at once could lead to resource exhaustion or when the order of operations matters.

What are promise-queue's main functionalities?

Creating a queue and adding tasks

This code demonstrates how to create a new queue with a maximum of 2 concurrent operations and an infinite queue size. It then adds an asynchronous function to the queue and logs the result when the promise resolves.

const Queue = require('promise-queue');
let maxConcurrent = 2;
let maxQueue = Infinity;
let queue = new Queue(maxConcurrent, maxQueue);

function someAsyncFunction() {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve('done');
    }, 1000);
  });
}

queue.add(someAsyncFunction).then(console.log);

Handling results and errors

This snippet shows how to handle both successful results and errors for tasks added to the queue. It uses `.then` for success callbacks and `.catch` for error handling.

queue.add(someAsyncFunction).then(result => {
  console.log('Result:', result);
}).catch(error => {
  console.error('Error:', error);
});

Getting the size of the queue

These lines of code demonstrate how to get the number of queued and pending promises in the queue. `getQueueLength` returns the number of queued (not yet started) tasks, while `getPendingLength` returns the number of tasks currently running.

console.log(queue.getQueueLength());
console.log(queue.getPendingLength());

Other packages similar to promise-queue

FAQs

Package last updated on 28 Jan 2018

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc