better-all
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseDocumentation
文档
- GitHub Repository: https://github.com/shuding/better-all
Note: This library is not yet indexed in DeepWiki or Context7.
注意:该库尚未在DeepWiki或Context7中建立索引。
better-all Library
better-all库
better-allPromise.allbetter-allPromise.allInstallation
安装
bash
pnpm add better-allbash
pnpm add better-allBasic 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 typedtypescript
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