Skip to content
Back to skills

318 Async Parallel F35a977f

ASecurity

> **错误 ID**: E001 > **频率**: 高 > **严重度**: 🔴 严重 ---

  • 9 stars
  • 0 votes
  • 0 copies
  • 0 views
  • Added October 11, 2026
developmentjavascriptjavagitapi

Works with

  • api
  • mcp

Security analysis

A100/100

Pro scans all 2 files and shows the line behind each finding

Scanned October 11, 2026

npx -y skills add tools-only/X-Skills --skill 318-async-parallel_f35a977f --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of 318 Async Parallel F35a977f?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for 318 Async Parallel F35a977f
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/tools-only-318-async-parallel-f35a977f/badge)](https://www.skillsdirectory.com/skills/tools-only-318-async-parallel-f35a977f)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
# 异步并行处理错误

> **错误 ID**: E001
> **频率**: 高
> **严重度**: 🔴 严重

---

## 📋 错误描述

**常见表现**:
- 多个独立 API 调用顺序执行
- 加载时间 = 单次时间 × 调用次数
- 性能极差(10-13x 慢)

**根本原因**:
- 使用 `for...of` + `await` 顺序执行
- 忽略了多个请求之间没有依赖关系
- 没有利用 JavaScript 的并发能力

---

## ❌ 错误示例

```javascript
// ❌ 错误:顺序执行 13 次搜索
for (const term of searchTerms) {
  const results = await githubProxy.searchRepositories(term);
  allResults.push(...results);
}
// 总耗时:13 × 2秒 = 26秒+
```

---

## ✅ 正确做法

```javascript
// ✅ 正确:并行执行所有搜索
const searchPromises = searchTerms.map(term =>
  githubProxy.searchRepositories(term)
    .then(results => ({ term, results, success: true }))
    .catch(error => {
      console.error(`Search failed for "${term}":`, error.message);
      return { term, results: [], success: false, error: error.message };
    })
);

const searchResults = await Promise.all(searchPromises);
// 总耗时:max(2秒) = 2秒
```

---

## 🔍 关键改进

1. ✅ 使用 `.map()` 创建 Promise 数组
2. ✅ 使用 `Promise.all()` 并行执行
3. ✅ 单个失败不影响其他(每个 Promise 内部处理错误)
4. ✅ 性能提升 10-13 倍

---

## 📌 自检清单

- [ ] 是否有多个独立的异步操作?
- [ ] 是否使用 `Promise.all()` 并行执行?
- [ ] 是否每个 Promise 内部处理错误?
- [ ] 是否避免使用 `for...of` + `await`?

---

## 🎯 适用场景

✅ **应该并行**:
- 多个独立 API 调用
- 批量数据库查询(无依赖关系)
- 多个文件读取操作
- 并行 MCP 工具调用

❌ **不应该并行**:
- 后续操作依赖前面结果(需要顺序执行)
- 需要限制并发数量(应使用 p-limit 等库)
- 数据库事务操作(需要保证顺序)

---

**返回**: [ERROR_CATALOG.md](../ERROR_CATALOG.md)

Files in this skill

  • README.md864 B
  • skill.md2 KB

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.