Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Apply 62 React and Next.js performance optimization rules from Vercel Engineering
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
rules/async-dependencies.md
1---2title: Dependency-Based Parallelization3impact: CRITICAL4impactDescription: 2-10× improvement5tags: async, parallelization, dependencies, better-all6---78## Dependency-Based Parallelization910For operations with partial dependencies, use `better-all` to maximize parallelism. It automatically starts each task at the earliest possible moment.1112**Incorrect (profile waits for config unnecessarily):**1314```typescript15const [user, config] = await Promise.all([16fetchUser(),17fetchConfig()18])19const profile = await fetchProfile(user.id)20```2122**Correct (config and profile run in parallel):**2324```typescript25import { all } from 'better-all'2627const { user, config, profile } = await all({28async user() { return fetchUser() },29async config() { return fetchConfig() },30async profile() {31return fetchProfile((await this.$.user).id)32}33})34```3536**Alternative without extra dependencies:**3738We can also create all the promises first, and do `Promise.all()` at the end.3940```typescript41const userPromise = fetchUser()42const profilePromise = userPromise.then(user => fetchProfile(user.id))4344const [user, config, profile] = await Promise.all([45userPromise,46fetchConfig(),47profilePromise48])49```5051Reference: [https://github.com/shuding/better-all](https://github.com/shuding/better-all)52