Resolve several promises using concurrency limit and failfast.
This library provides concurrentPromiseAll
and concurrentPromiseAllSettled
.
They work the same as Promise.all
and Promise.allSettled
, enhanced with options that allow you to configure a concurrency limit and failfast.
$ yarn add concurrent-promise-all
import { concurrentPromiseAll } from "concurrent-promise-all";
const result = await concurrentPromiseAll(
[
() => axios("http://my-api/foo"),
() => axios("http://my-api/bar"),
() => axios("http://my-api/foobar"),
],
{ maxConcurrent: 2 }
);
console.log(result);
[
{ /* axios response foo */ },
{ /* axios response bar */ },
{ /* axios response foobar */ },
];
import { concurrentPromiseAllSettled } from "concurrent-promise-all";
const result = await concurrentPromiseAllSettled(
[
() => axios("http://my-api"),
() => axios("http://my-api"),
() => axios("http://my-api"),
],
{ maxConcurrent: 2 }
);
console.log(result);
[
{
status: "fulfilled",
value: {
/* axios response */
},
},
{
status: "fulfilled",
value: {
/* axios response */
},
},
{
status: "fulfilled",
value: {
/* axios response */
},
},
];
It also works if you don't put "promise function" in the array. BUT rate limiting won't work if concurrent-promise-all can't trigger promise itself.
concurrentPromiseAllSettled(
[() => axios("http://my-api"), axios("http://my-api"), "foo"],
{ maxConcurrent: 2 }
);
[
{
status: "fulfilled",
value: { /* axios response */ },
},
{
status: "fulfilled",
value: { /* axios response */ },
},
{ status: "fulfilled", value: "foo" },
];
Example on promise reject.
concurrentPromiseAllSettled(
[
() => axios("http://my-api-that-crashes"),
() => {
throw "oops no promise and error";
},
async () => {
throw "oops error in promise";
},
() => axios("http://my-api-that-works"),
],
{ maxConcurrent: 3 }
);
[
{
status: "rejected",
reason: {
/* error */
},
},
{
status: "rejected",
reason: {
/* error */
},
},
{
status: "rejected",
reason: {
/* error */
},
},
{
status: "fulfilled",
value: {
/* axios response */
},
},
];
And fail fast, after first promise reject. Keep it mind that you can have as many rejected
as maxConcurrent
, since promise are executed "simultaneously".
concurrentPromiseAllSettled(
[
() => axios("http://my-api-that-crashes"),
() => {
throw "oops no promise and error";
},
async () => {
throw "oops error in promise";
},
() => axios("http://my-api-that-works"),
],
{ maxConcurrent: 2 }
);
[
{
status: "rejected",
reason: { /* error */ },
},
{
status: "rejected",
reason: { /* error */ },
},
{
status: "skipped",
promiseFunction: { /* not executed promise function */ },
},
{
status: "skipped",
promiseFunction: { /* not executed promise function */ },
},
];