import { bulkhead } from "@daiso-tech/core/async";
import { AsyncHooks } from "@daiso-tech/core/utilities";
const fetchData = new AsyncHooks(async (url: string): Promise<unknown> => {
const response = await fetch(url);
const json = await response.json();
return json;
}, [
bulkhead()
]);
// Will run only 25 promises concurrently by default.
await Promise.all(Array(50).fill("").map(() => fetchData.invoke("URL")));
The
bulkhead
middlewares ensures that a given amount of Promiselike |PromiseLike
objects run at the same time concurrently and the rest will be queued up. You can providesettings.maxCapacity
IMPORT_PATH:
"@daiso-tech/core/async"