Nâng CaoJavaScript iconJavaScript

async/await với parallel execution như thế nào?

await tuần tự từng operation sẽ chậm hơn.

Chạy song song bằng cách tạo tất cả Promises trước rồi mới await.

javascript
// Sequential — chậm (mỗi request phải chờ cái trước)
const user = await fetchUser(id);
const posts = await fetchPosts(id); // chờ user xong mới gọi

// Parallel — nhanh hơn
const [user, posts] = await Promise.all([
  fetchUser(id),
  fetchPosts(id),
]);

// Tránh await trong loop (serial)
for (const id of ids) {
  await fetchItem(id); // BAD: 1 by 1
}

// Dùng Promise.all cho parallel
const items = await Promise.all(ids.map(id => fetchItem(id)));

Xem toàn bộ JavaScript cùng filter theo level & chủ đề con.

Mở danh sách JavaScript