better-all

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Documentation

文档

Note: This library is not yet indexed in DeepWiki or Context7.
注意:该库尚未在DeepWiki或Context7中建立索引。

better-all Library

better-all库

better-all
provides
Promise.all
with automatic dependency optimization. Instead of manually analyzing which tasks can run in parallel, tasks declare dependencies inline and execution is automatically optimized.
better-all
Promise.all
提供了自动依赖优化功能。无需手动分析哪些任务可以并行运行,只需在任务中内联声明依赖,执行过程就会自动优化。

Installation

安装

bash
pnpm add better-all
bash
pnpm add better-all

Basic Usage

基础用法

typescript
import { all } from "better-all";

const results = await all({
  // Independent tasks run in parallel
  fetchUser: () => fetchUser(userId),
  fetchPosts: () => fetchPosts(userId),

  // Dependent task waits automatically
  combined: async (ctx) => {
    const user = await ctx.$.fetchUser;
    const posts = await ctx.$.fetchPosts;
    return { user, posts };
  },
});

// results.fetchUser, results.fetchPosts, results.combined all typed
typescript
import { all } from "better-all";

const results = await all({
  // 独立任务将并行运行
  fetchUser: () => fetchUser(userId),
  fetchPosts: () => fetchPosts(userId),

  // 依赖任务会自动等待前置任务完成
  combined: async (ctx) => {
    const user = await ctx.$.fetchUser;
    const posts = await ctx.$.fetchPosts;
    return { user, posts };
  },
});

// results.fetchUser、results.fetchPosts、results.combined均带有类型定义

Key Advantage: Automatic Optimization

核心优势:自动优化

typescript
// Manual approach - error-prone
const [user, posts] = await Promise.all([fetchUser(), fetchPosts()]);
const profile = await buildProfile(user, posts);
const [feed, stats] = await Promise.all([
  buildFeed(profile, posts),
  buildStats(profile),
]);

// better-all - dependencies declared, execution optimized
const results = await all({
  user: () => fetchUser(),
  posts: () => fetchPosts(),
  profile: async (ctx) => buildProfile(await ctx.$.user, await ctx.$.posts),
  feed: async (ctx) => buildFeed(await ctx.$.profile, await ctx.$.posts),
  stats: async (ctx) => buildStats(await ctx.$.profile),
});
typescript
// 手动实现方式 - 容易出错
const [user, posts] = await Promise.all([fetchUser(), fetchPosts()]);
const profile = await buildProfile(user, posts);
const [feed, stats] = await Promise.all([
  buildFeed(profile, posts),
  buildStats(profile),
]);

// better-all实现 - 声明依赖后,执行过程自动优化
const results = await all({
  user: () => fetchUser(),
  posts: () => fetchPosts(),
  profile: async (ctx) => buildProfile(await ctx.$.user, await ctx.$.posts),
  feed: async (ctx) => buildFeed(await ctx.$.profile, await ctx.$.posts),
  stats: async (ctx) => buildStats(await ctx.$.profile),
});

Type Inference

类型推断

Results are fully typed based on task return types:
typescript
const results = await all({
  count: () => Promise.resolve(42),
  name: () => Promise.resolve("test"),
  combined: async (ctx) => ({
    count: await ctx.$.count,
    name: await ctx.$.name,
  }),
});

// TypeScript knows:
// results.count: number
// results.name: string
// results.combined: { count: number; name: string }
返回结果会根据任务的返回类型自动生成完整的类型定义:
typescript
const results = await all({
  count: () => Promise.resolve(42),
  name: () => Promise.resolve("test"),
  combined: async (ctx) => ({
    count: await ctx.$.count,
    name: await ctx.$.name,
  }),
});

// TypeScript能够识别:
// results.count: number
// results.name: string
// results.combined: { count: number; name: string }

References

参考资料

  • For complex DAG patterns, see dag-patterns.md
  • 如需了解复杂DAG模式,请查看dag-patterns.md