Provides Promise.all and Promise.allSettled variants that limit the number of simultaneously pending promises.
npm install capped-promiseProvides Promise.all and Promise.allSettled variants that limit the number of simultaneously pending promises. This
is useful e.g. when you need to make thousands of requests to a single server but do not want to hit it with all
requests at once. Instead you might want to have at most 10 requests pending simultaneously and whenever one settles,
the next one is initiated automatically.
This is a ES2019 CommonJS module, that works exactly the same way in CommonJS and ES modules (because there's only an
unnamed default export). TypeScript typings are provided.
npm install capped-promise
``ts
// Only necessary in node, fetch is natively available in a browser.
import fetch from "node-fetch";
import CappedPromise from "capped-promise";
const getText = async (url: string) => {
const response = await fetch(url);
if (!response.ok) {
throw new Error("Unexpected response");
}
return await response.text();
};
const cssUrls = [
"https://fonts.googleapis.com/css2?family=Roboto",
"https://fonts.googleapis.com/css2?family=Open+Sans",
"https://fonts.googleapis.com/css2?family=Poppins",
];
// Promise.all
// We pass already pending promises.
const cssPromises = cssUrls.map((url) => getText(url));
// All requests are made at the same time.
const promiseResults = await Promise.all(cssPromises);
// CappedPromise.all
// We pass promise-creating functions instead of promises.
const createCssPromises = cssUrls.map((url) => () => getText(url));
// We allow at most 2 simultaneous requests. So, in the beginning the first
// two promises are created right away. As soon as one of those is fulfilled,
// the third is automatically created. When the responses for all requests are
// in, the returned promise fulfills.
const cappedResults = await CappedPromise.all(2, createCssPromises);
`
Promise.all and Promise.allSettled accept iterables of awaitables (usually promises). By its very nature, once aPromise
promise has been created, it is in the state pending. That is, the underlying operation is already running. So, if you
create e.g. 1000 objects by calling fetch()await
repeatedly (without ing them), all those requests will be made quasi simultaneously, if you pass them toPromise.all and then await the result. This is why CappedPromise.all and CappedPromise.allSettled differ fromPromise
their counterparts as follows:
- Instead of awaitables, they accept parameterless functions that are expected to create and return awaitables. The
CappedPromise implementation will then call these functions to create new awaitables as necessary.maxPending
- The first parameter is , which specifies how many promises can at most be pending simultaneously.
Otherwise, CappedPromise.all and CappedPromise.allSettled follow their Promise counterparts.
Again, the CappedPromise behavior follows the Promise behavior as much as possible. However, due to the differentCappedPromise
interface there is the following additional concern: If a function passed to a method throws right away,() => { throw new Error("Oops!"); }
e.g. then this is treated as an unintended catastrophic failure and is propagatedallSettled
as a rejection immediately, even if the function was passed to . If this is not what you expect, youPromise
should return a rejecting , as follows: () => Promise.reject(new Error("Oops!"))`.