2.0.0 • Published 3 years ago

max-concurrency v2.0.0

Weekly downloads
245
License
MIT
Repository
github
Last release
3 years ago

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).

2.0.0

3 years ago

1.5.7

3 years ago

1.5.6

4 years ago

1.5.5

4 years ago

1.5.4

4 years ago

1.5.3

4 years ago

1.5.2

4 years ago

1.5.1

4 years ago

1.5.0

4 years ago

1.4.4

4 years ago

1.4.3

5 years ago

1.4.1

5 years ago

1.4.0

5 years ago

1.3.10

5 years ago

1.3.9

6 years ago

1.3.8

6 years ago

1.3.7

6 years ago

1.3.6

6 years ago

1.3.5

6 years ago

1.3.4

6 years ago

1.3.3

6 years ago

1.3.2

6 years ago

1.3.1

6 years ago

1.3.0

6 years ago

1.2.0

6 years ago

1.1.0

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago