Course contentsShow
← JavaScript
Lesson 1386 of 2,72330. Async/AwaitFree lesson

Sequential Awaits When Parallel Would Work

Recognize the performance cost of awaiting independent operations in sequence instead of parallel.

Sequential Awaits When Parallel Would Work

You'll learn to spot a common performance trap where you await independent operations one-by-one instead of running them together.

The Performance Trap

When you write multiple await statements in sequence, JavaScript pauses at each one, waiting for it to complete before moving to the next. This is necessary when later operations depend on earlier results—but disastrous when they don't.

Think of it like ordering coffee: if you order three drinks and wait for each one to be made before ordering the next, you're wasting time. You should order all three at once and wait for them together.

The Anti-Pattern in Action

async function fetchUserData() {
  const profile = await fetchProfile();     // Waits ~1 second
  const settings = await fetchSettings();   // Waits ~1 second
  const friends = await fetchFriends();     // Waits ~1 second
  return { profile, settings, friends };
  // Total time: ~3 seconds
}

If these three fetches don't depend on each other, you've just tripled your wait time for no reason.

The Fix

async function fetchUserData() {
  const [profile, settings, friends] = await Promise.all([
    fetchProfile(),
    fetchSettings(),
    fetchFriends()
  ]);
  return { profile, settings, friends };
  // Total time: ~1 second (the slowest operation)
}

By recognizing that these operations are independent, you start them all immediately and await their combined result.

Key Takeaway: Always ask yourself "does this await depend on the previous one?" If not, consider running them in parallel with Promise.all to avoid unnecessary sequential waiting.