Socket
Socket
Sign inDemoInstall

promise-limit

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

promise-limit

limits calls to functions that return promises


Version published
Weekly downloads
315K
increased by5.97%
Maintainers
1
Weekly downloads
 
Created

What is promise-limit?

The promise-limit npm package allows you to limit the number of concurrent promises that are executed. This is particularly useful when dealing with a large number of asynchronous operations that could overwhelm system resources if executed all at once.

What are promise-limit's main functionalities?

Limit Concurrent Promises

This feature allows you to limit the number of concurrent promises. In this example, only 2 promises will run at the same time. The `limit.map` function takes an array of tasks and ensures that only the specified number of tasks run concurrently.

const limit = require('promise-limit')(2);

const tasks = [
  () => new Promise(resolve => setTimeout(() => resolve('Task 1'), 1000)),
  () => new Promise(resolve => setTimeout(() => resolve('Task 2'), 500)),
  () => new Promise(resolve => setTimeout(() => resolve('Task 3'), 300)),
  () => new Promise(resolve => setTimeout(() => resolve('Task 4'), 200))
];

limit.map(tasks, task => task()).then(results => {
  console.log(results); // ['Task 1', 'Task 2', 'Task 3', 'Task 4']
});

Limit Concurrent Promises with Error Handling

This feature demonstrates how to handle errors when limiting concurrent promises. If any promise rejects, the error will be caught and handled appropriately.

const limit = require('promise-limit')(2);

const tasks = [
  () => new Promise((resolve, reject) => setTimeout(() => reject('Error in Task 1'), 1000)),
  () => new Promise(resolve => setTimeout(() => resolve('Task 2'), 500)),
  () => new Promise(resolve => setTimeout(() => resolve('Task 3'), 300)),
  () => new Promise(resolve => setTimeout(() => resolve('Task 4'), 200))
];

limit.map(tasks, task => task()).then(results => {
  console.log(results); // ['Error in Task 1', 'Task 2', 'Task 3', 'Task 4']
}).catch(error => {
  console.error(error);
});

Other packages similar to promise-limit

Keywords

FAQs

Package last updated on 28 Jul 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