Socket
Socket
Sign inDemoInstall

@supercharge/promise-pool

Package Overview
Dependencies
Maintainers
3
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@supercharge/promise-pool

Map-like, concurrent promise processing for Node.js


Version published
Weekly downloads
416K
increased by8.24%
Maintainers
3
Weekly downloads
 
Created

What is @supercharge/promise-pool?

@supercharge/promise-pool is an npm package that allows you to process a large number of promises concurrently with a specified limit on the number of promises that can run at the same time. This helps in managing resources efficiently and avoiding overwhelming the system.

What are @supercharge/promise-pool's main functionalities?

Concurrent Processing

This feature allows you to process a list of items concurrently with a specified limit on the number of concurrent operations. In this example, the concurrency limit is set to 5.

const { PromisePool } = require('@supercharge/promise-pool');

async function processItems(items) {
  const { results, errors } = await PromisePool
    .for(items)
    .withConcurrency(5)
    .process(async item => {
      // Process each item
      return await processItem(item);
    });

  console.log('Results:', results);
  console.log('Errors:', errors);
}

async function processItem(item) {
  // Simulate async processing
  return new Promise(resolve => setTimeout(() => resolve(item), 1000));
}

processItems([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);

Error Handling

This feature demonstrates how to handle errors that occur during the processing of items. Errors are collected and can be reviewed after the processing is complete.

const { PromisePool } = require('@supercharge/promise-pool');

async function processItems(items) {
  const { results, errors } = await PromisePool
    .for(items)
    .withConcurrency(5)
    .process(async item => {
      if (item % 2 === 0) {
        throw new Error('Even number error');
      }
      return await processItem(item);
    });

  console.log('Results:', results);
  console.log('Errors:', errors);
}

async function processItem(item) {
  // Simulate async processing
  return new Promise(resolve => setTimeout(() => resolve(item), 1000));
}

processItems([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);

Dynamic Concurrency

This feature allows you to dynamically set the concurrency level based on certain conditions. In this example, the concurrency is set to 10 if the number of items is greater than 5, otherwise, it is set to 2.

const { PromisePool } = require('@supercharge/promise-pool');

async function processItems(items) {
  const concurrency = items.length > 5 ? 10 : 2;
  const { results, errors } = await PromisePool
    .for(items)
    .withConcurrency(concurrency)
    .process(async item => {
      return await processItem(item);
    });

  console.log('Results:', results);
  console.log('Errors:', errors);
}

async function processItem(item) {
  // Simulate async processing
  return new Promise(resolve => setTimeout(() => resolve(item), 1000));
}

processItems([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);

Other packages similar to @supercharge/promise-pool

Keywords

FAQs

Package last updated on 10 Feb 2023

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