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`);