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

Choosing Between Sequential and Parallel

Determine when operations must run sequentially versus when parallel execution is safe and beneficial.

Choosing Between Sequential and Parallel

You'll learn: how to decide whether async operations should execute one-after-another or all-at-once based on dependencies and performance needs.

The Decision Framework

Not all async operations are created equal. Some must run sequentially because later operations depend on earlier results. Others are completely independent and can safely run in parallel for better performance.

Think of it like cooking: you must wait for water to boil before adding pasta (sequential), but you can chop vegetables while the water heats (parallel).

When Sequential is Required

Use sequential await statements when:

  • Data dependency exists: Operation B needs the result from Operation A
  • Order matters: Writing to a database record before reading it
  • Side effects must sequence: Creating a user account before sending a welcome email
async function sequentialExample() {
  const userId = await createUser();
  const profile = await createProfile(userId); // needs userId!
  return profile;
}

When Parallel is Safe and Better

Use Promise.all or start promises before awaiting when:

  • Operations are independent: No operation needs another's result
  • Performance matters: Faster total execution time
  • No ordering requirements: Operations can complete in any order
async function parallelExample() {
  // Start all three immediately
  const [weather, news, stocks] = await Promise.all([
    fetchWeather(),
    fetchNews(),
    fetchStocks()
  ]);
  return { weather, news, stocks };
}

The Key Question

Ask yourself: "Does this operation need to wait for the previous one to finish, or can it start right now?" If it can start now, make it parallel.

Key Takeaway: Choose sequential execution when operations have dependencies or ordering requirements; choose parallel execution when operations are independent to maximize performance.