Start early, await late

Start independent work at once; chain only real dependencies.

better-allgithub.com/shuding
For operations with partial dependencies, use `better-all` to maximize parallelism. It automatically starts each task at the earliest possible moment.
Rule: async-dependenciesView source ↗

From vercel-react-best-practices: async-parallel, async-dependencies and 2 more.

Three independent fetches

Press Run, then drag any duration.

Sequential awaitBad
await a; await b; await c
Promise.allGood
await Promise.all([a, b, c])

const [user, posts, comments] = await Promise.all([
  fetchUser(),
  fetchPosts(),
  fetchComments()
])

Partial dependencies

Drag config past user. Only profile depends on user.

All, then profileBad
await Promise.all([user, config])
Chain off userPromiseGood
userP.then(fetchProfile)

const userPromise = fetchUser()
const profilePromise = userPromise.then(user => fetchProfile(user.id))
const [user, config, profile] = await Promise.all([
  userPromise,
  fetchConfig(),
  profilePromise
])

Defer the await to its branch

Send a missing id. Only inv_101 to inv_103 exist.

Permissions firstBad
–
Resource firstGood
–

async function updateResource(resourceId: string, userId: string) {
  const resource = await getResource(resourceId)
  if (!resource) {
    return { error: 'Not found' }
  }

  const permissions = await fetchPermissions(userId)
  if (!permissions.canEdit) {
    return { error: 'Forbidden' }
  }

  return await updateResourceData(resource, permissions)
}

Why it matters, pros and cons

Why it matters

Serial awaits add up; parallel ones cost only the slowest call.

Pros

  • 2-10× faster multi-call routes
  • No new infrastructure, just Promise.all
  • Early exits stay cheap

Cons

  • Promise.all rejects on the first failure
  • Concurrent calls can exhaust a pool or rate limit
  • Order-dependent writes must stay sequential