Loading...
Loading...
Use the better-all library for Promise.all with automatic DAG-based dependency optimization and full type inference. Use when parallelizing async operations with complex dependencies.
npx skill4agent add casper-studios/casper-marketplace better-allbetter-allPromise.allpnpm add better-allimport { 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 }