[ PROMPT_NODE_25184 ]
async-dependencies
[ SKILL_DOCUMENTATION ]
## 基于依赖的并行化
对于存在部分依赖的操作,使用 `better-all` 来最大化并行处理能力。它会自动在最早可能的时刻启动每个任务。
**错误做法(profile 不必要地等待 config):**
typescript
const [user, config] = await Promise.all([
fetchUser(),
fetchConfig()
])
const profile = await fetchProfile(user.id)
**正确做法(config 和 profile 并行运行):**
typescript
import { all } from 'better-all'
const { user, config, profile } = await all({
async user() { return fetchUser() },
async config() { return fetchConfig() },
async profile() {
return fetchProfile((await this.$.user).id)
}
})
参考:[https://github.com/shuding/better-all](https://github.com/shuding/better-all)