Socket
Book a DemoInstallSign in
Socket

max-concurrency

Package Overview
Dependencies
Maintainers
1
Versions
31
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

max-concurrency

Similar to Promise.all() but limits the number of promises in flight.

latest
Source
npmnpm
Version
2.0.0
Version published
Maintainers
1
Created
Source

max-concurrency

Build Coverage Status npm version Package Size

Sometimes you don't want to use Promise.all because sometimes you don't want all the promises you pass to it to be all in-flight at the same time. This package will allow you to limit how many promises are in-flight at the same time. The reason this API takes a list of promise providing functions is because if you passed it an array of promises, those promises would all already be in-flight.

import { Concurrency } from 'max-concurrency'

expect(await Concurrency.all({
    promiseProviders: [
        () => Promise.resolve(1),
        () => Promise.resolve(2),
        () => Promise.resolve(3),
        () => Promise.resolve(4)
    ],
    maxConcurrency: 3
})).toEqual([1, 2, 3, 4]);

This package also allows more control over error handling. Promise.all() will reject/throw when it encounters an error (ie. a rejected promise). This package will default to that same behavior but will also allow you to define an error mapper. This will map errors to "errored values" in the results and this allows for the rest of the promises to continue.

expect(await Concurrency.all({
    promiseProviders: [
        () => Promise.resolve(1),
        () => Promise.reject(new Error("the error")),
        () => Promise.resolve(3)
    ],
    mapErrors: ({ message }) => ({ errorMessage: message })
})).toEqual([1, { errorMessage: "the error" }, 3]);

Just like Promise.all, this package will return the resolved values in the same order as the array of promise providers passed to it (not the order that the promises resolved in).

Keywords

async

FAQs

Package last updated on 14 May 2021

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

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.