Common Pitfalls: Sequential vs Concurrent
You'll learn how to avoid accidentally running promises sequentially when you intend them to run concurrently — a common mistake that costs performance.
The Core Problem
When using Promise.all(), the goal is usually to run multiple async operations in parallel (concurrently). However, developers often accidentally force them to run sequentially by creating promises inside the Promise.all() call instead of passing already-started promises.
The Mistake
// ❌ SEQUENTIAL - promises created inside Promise.all
Promise.all([
fetch('/api/user'), // starts first
fetch('/api/posts'), // starts after user completes
fetch('/api/comments') // starts after posts complete
]);
Wait — this looks wrong! The issue is more subtle. If you create promise-returning functions inside Promise.all, they start immediately. But if you create them in sequence first:
// ❌ SEQUENTIAL - promises start one by one
const user = await fetch('/api/user');
const posts = await fetch('/api/posts');
const comments = await fetch('/api/comments');
const results = Promise.all([user, posts, comments]);
Here, each await blocks the next operation from starting.
The Correct Pattern
// ✅ CONCURRENT - start all promises first, then wait
const userPromise = fetch('/api/user');
const postsPromise = fetch('/api/posts');
const commentsPromise = fetch('/api/comments');
const results = await Promise.all([userPromise, postsPromise, commentsPromise]);
The Analogy
Think of ordering food at three different restaurants. Sequential is calling the first, waiting for delivery, then calling the second. Concurrent is calling all three at once, then waiting for all deliveries together.
Key Takeaway: Always start your promises before passing them to Promise.all() — they should already be "in flight" to achieve true concurrency.