Parallel Execution with Dependent Results
What you'll learn: How to mix parallel and sequential async operations when some tasks depend on earlier results.
The Challenge
Not all async work fits neatly into "all parallel" or "all sequential" categories. Often you'll face scenarios where some operations can run simultaneously, but others need the results from the first batch before they can start.
The Strategy
The key is to batch independent operations together, await them in parallel, then use those results to launch the next wave of async work.
Here's the pattern:
async function fetchUserDashboard(userId) {
// Step 1: These are independent—run in parallel
const [user, settings] = await Promise.all([
fetchUser(userId),
fetchSettings(userId)
]);
// Step 2: Now we need user data, but these two are independent of each other
const [posts, friends] = await Promise.all([
fetchPosts(user.id),
fetchFriends(user.id)
]);
return { user, settings, posts, friends };
}
Notice the structure: two parallel batches separated by an await. The second batch depends on data from the first (the user.id), but within each batch, operations run concurrently.
The Analogy
Think of cooking a meal: you can chop vegetables and boil water simultaneously (parallel). But you can't add pasta to the water until it's boiling (dependent). Once pasta is cooking, you can make sauce and grate cheese at the same time (parallel again).
When to Use This
Anytime you have a dependency graph where some branches are independent. Start all independent operations at once, await them together, then use their results for the next tier.
Key Takeaway: Maximize performance by batching independent async operations with Promise.all, even when overall workflow has sequential dependencies—parallelize within each dependency tier.