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