npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@imrandil/concurrent-promise-batcher

v1.0.4

Published

A utility for executing asynchronous tasks concurrently in batches, providing better control over resource usage and improved performance.

Downloads

5

Readme

Concurrent Promise Batcher

Concurrent Promise Batcher is a Node.js utility for executing asynchronous tasks concurrently in batches, providing better control over resource usage and improved performance. This package allows you to fetch data concurrently for multiple batches while managing the maximum number of concurrent promises being executed.

Installation

npm install @imrandil/concurrent-promise-batcher

Usage

const concurrentPromise = require('@imrandil/concurrent-promise-batcher');

// Define a function to execute for each item in the batch
async function fetchData(item) {
    // Perform asynchronous operation (e.g., fetching data from an API)
    // Return a Promise
}

// Define your batches
const batches = [/* Array of items representing each batch */];

// Set the maximum number of concurrent promises to execute
const batchConcurrency = 5;

// Execute promises concurrently for batches
concurrentPromise(batchConcurrency, batches, fetchData)
    .then((result) => {
        console.log('Results:', result.results);
        console.log('Execution Time:', result.executionTime);
        console.log('Number of Batches Processed:', result.numBatchesProcessed);
        console.log('Average Execution Time per Batch:', result.avgExecutionTimePerBatch);
    })
    .catch((error) => {
        console.error('Error:', error.message);
    });

One of the use cases ->

const concurrentPromise = require('@imrandil/concurrent-promise-batcher');

// Function to fetch data from an API
async function fetchDataFromAPI(url) {
    const response = await fetch(url);
    return response.json();
}

// Array of API endpoints
const apiEndpoints = [
    'https://hotels4.p.rapidapi.com/v2/get-meta-data',
    'https://api.publicapis.org/entries',
    'https://api.coindesk.com/v1/bpi/currentprice.json',
    'https://www.boredapi.com/api/activity',
    'https://dog.ceo/api/breeds/image/random'
];

// Define the maximum concurrency level
const maxConcurrency = 3; // For example, fetching data from 3 APIs concurrently

// Fetch data concurrently from multiple APIs
concurrentPromise(maxConcurrency, apiEndpoints, fetchDataFromAPI, true)
    .then(({ results, executionTime, numBatchesProcessed, avgExecutionTimePerBatch }) => {
        console.log('Results:', results);
        console.log('Execution time:', executionTime);
        console.log('Number of batches processed:', numBatchesProcessed);
        console.log('Average execution time per batch:', avgExecutionTimePerBatch);
    })
    .catch(error => {
        console.error('Error:', error);
    });

Parameters

  • batchConcurrency: The maximum number of concurrent promises to execute. Must be a positive integer.
  • batches: An array of items representing each batch. Must be a non-empty array.
  • fn: The function to execute concurrently for each batch item. Must be a function.
  • settled: Whether to use Promise.allSettled or Promise.all. Default is false (Promise.all).

Examples

  • Fetching data from multiple APIs concurrently.
  • Processing multiple files concurrently.
  • Bulk operations on a database concurrently.

Using Promise.all vs Promise.allSettled

By default, concurrentPromise uses Promise.all to await the fulfillment of all promises in each batch. This means that if any promise rejects (encounters an error), the entire batch will fail and the rejection will be propagated to the caller.

If you want to handle individual promise rejections separately or continue processing even if some promises fail, you can set the settled parameter to true. This will make concurrentPromise use Promise.allSettled instead. With Promise.allSettled, the returned promise will fulfill with an array of objects representing the status of each promise, whether fulfilled or rejected.

License

MIT