返回 AiToEarn
async-parallel.md
1 ---
2 title: Promise.all() for Independent Operations
3 impact: CRITICAL
4 impactDescription: 2-10× improvement
5 tags: async, parallelization, promises, waterfalls
6 ---
7
8 ## Promise.all() for Independent Operations
9
10 When async operations have no interdependencies, execute them concurrently using `Promise.all()`.
11
12 **Incorrect (sequential execution, 3 round trips):**
13
14 ```typescript
15 const user = await fetchUser()
16 const posts = await fetchPosts()
17 const comments = await fetchComments()
18 ```
19
20 **Correct (parallel execution, 1 round trip):**
21
22 ```typescript
23 const [user, posts, comments] = await Promise.all([
24 fetchUser(),
25 fetchPosts(),
26 fetchComments()
27 ])
28 ```
29
29 lines MARKDOWN