关于 better-all
better-all 是一个 TypeScript 库,可声明任务依赖并据此安排执行。它适合希望并行运行任务、又不想手动编排依赖阶段的开发者。
主要功能
- 01内联声明任务依赖
- 02自动优化并行执行
- 03推断结果类型
适用场景
- 01并行运行互不依赖的数据获取任务
- 02安排多步操作中的依赖任务
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ casper-studios/ casper-marketplace --skill better-all安装后在 Claude Code 中输入/better-all确认已加载。
附带文件references/dag-patterns.md
详细介绍
文档
- GitHub 仓库:https://github.com/shuding/better-all
注意:此库尚未被 DeepWiki 或 Context7 收录。
better-all 库
better-all 提供了 Promise.all,并能自动优化依赖关系。你无需手动分析哪些任务可以并行运行,只需在任务中声明依赖关系,即可自动优化执行。
安装
pnpm add better-all
基本用法
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
主要优势:自动优化
// 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),
});
类型推断
根据任务的返回类型,结果会得到完整的类型定义:
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 }
参考资料
- 复杂的 DAG 模式请参阅 dag-patterns.md
评论
支持 Markdown