parallel()
parallel<
T>(functions,concurrency?):Promise<T[]>
Executes multiple async functions in parallel with concurrency control.
Results preserve input order. Fails fast like Promise.all().
Type Parametersβ
T: Tβ
The return type of the functions.
Parametersβ
functions: () => Promise<T>[]β
Array of async functions to execute.
concurrency?: number = Infinityβ
Maximum number of concurrent executions. Defaults to Infinity.
Returns: Promise<T[]>β
Promise that resolves to array of results in original order.
Throwsβ
Rejects if any function rejects (remaining operations are aborted).
Default Valueβ
Infinity
Sinceβ
1.1.0
Performanceβ
Early abort on first error prevents unnecessary work.
Also known asβ
forEach (Effect) Β· parallel (Radashi) Β· β (Lodash, es-toolkit, Remeda, Ramda, Modern Dash, Antfu)
Exampleβ
const results = await parallel([
() => fetchUser(1),
() => fetchUser(2),
() => fetchUser(3)
], 2);
How it works?β
Executes async functions in parallel with concurrency control. Limits how many functions run simultaneously β results preserve original order.
Concurrency Visualizationβ
Fail-Fast Behaviorβ
On first error, remaining operations are aborted:
Use Casesβ
Control API rate limits πβ
Execute multiple API calls with controlled concurrency to respect rate limits. Essential for working with APIs that have strict rate limiting.
// Controlled API calls to respect rate limits
const userData = await parallel(
[
() => fetchUser(1),
() => fetchUser(2),
() => fetchUser(3),
() => fetchUser(4),
() => fetchUser(5),
],
2
); // Max 2 concurrent requests
console.log("Users loaded:", userData);
Manage resource-intensive operationsβ
Limit concurrent operations to prevent system overload. Critical for operations that consume significant system resources.
// Controlled image processing
const processedImages = await parallel(
[
() => processImage("image1.jpg"),
() => processImage("image2.jpg"),
() => processImage("image3.jpg"),
() => processImage("image4.jpg"),
],
2
); // Process max 2 images at a time
console.log("Images processed:", processedImages);
Optimize database operationsβ
Execute database queries with controlled concurrency for optimal performance. Essential for maintaining database performance and preventing connection exhaustion.
// Controlled database operations
const results = await parallel(
[
() => db.query("SELECT * FROM users WHERE active = 1"),
() => db.query("SELECT * FROM posts WHERE published = 1"),
() => db.query("SELECT * FROM comments WHERE approved = 1"),
() => db.query("SELECT * FROM categories WHERE visible = 1"),
],
3
); // Max 3 concurrent queries
console.log("Database queries completed:", results);
Handle file operations efficientlyβ
Process multiple files with controlled concurrency to prevent I/O overload. Critical for file processing systems and batch operations.
// Controlled file processing
const processedFiles = await parallel(
[
() => processFile("file1.txt"),
() => processFile("file2.txt"),
() => processFile("file3.txt"),
() => processFile("file4.txt"),
() => processFile("file5.txt"),
],
2
); // Process max 2 files at a time
console.log("Files processed:", processedFiles);
Implement progressive loadingβ
Load content progressively with controlled concurrency for better user experience. Essential for large applications with multiple content sections.
// Progressive content loading
const content = await parallel(
[
() => loadHeaderContent(),
() => loadSidebarContent(),
() => loadMainContent(),
() => loadFooterContent(),
],
2
); // Load max 2 sections at a time
renderPage(content);
Scrape pages with politenessβ
Crawl multiple URLs while limiting concurrent requests to avoid overwhelming servers. Essential for web scraping, link checking, or sitemap validation.
const urls = [
"https://example.com/page1",
"https://example.com/page2",
// ... 100 more URLs
];
const results = await parallel(
urls.map((url) => () => fetch(url).then((r) => r.text())),
5 // Max 5 concurrent requests - be polite!
);
console.log(`Scraped ${results.length} pages`);
Fetch lab results in parallel for patient dashboardsβ
Fetch multiple lab test results concurrently with controlled parallelism. Essential for healthcare portals displaying comprehensive patient diagnostics.
const patientTests = [
{ testId: "CBC-001", type: "Complete Blood Count" },
{ testId: "LFT-002", type: "Liver Function" },
{ testId: "RFT-003", type: "Renal Function" },
{ testId: "TFT-004", type: "Thyroid Function" },
{ testId: "LIP-005", type: "Lipid Panel" },
{ testId: "GLU-006", type: "Glucose Tolerance" },
];
// Fetch results with concurrency limit to avoid overwhelming lab API
const labResults = await parallel(
patientTests.map((test) => async () => {
const result = await labAPI.getResult(test.testId);
return {
...test,
result: result.values,
status: result.abnormal ? "ABNORMAL" : "NORMAL",
date: result.completedAt,
};
}),
3 // Max 3 concurrent requests to lab system
);
// Flag abnormal results for physician review
const abnormalResults = labResults.filter((r) => r.status === "ABNORMAL");
console.log(`${abnormalResults.length} results require review`);
Preload assets for a PWA with controlled concurrencyβ
Cache critical assets in parallel during service worker installation. Essential for PWA offline-first strategies with large asset lists.
const criticalAssets = [
"/index.html", "/app.js", "/styles.css",
"/icons/logo.svg", "/fonts/inter.woff2",
"/api/config", "/api/translations/en",
];
const preloadAll = async () => {
const cache = await caches.open("v1");
await parallel(
criticalAssets.map((url) => async () => {
const response = await fetch(url);
await cache.put(url, response);
}),
3 // Max 3 concurrent fetches
);
};