API reference › @evolu/common › Task › all
Call Signature
function all<TTasks>(
tasks: TTasks,
options: AllOptions,
): Task<
void,
InferTaskErr<TTasks[number]>,
ParameterIntersection<
TTasks[number] extends TTask
? TTask extends AnyTask
? (deps: InferTaskDeps<TTask>) => void
: never
: never
>
>;
Defined in: packages/common/src/Task.ts:3030
Runs Tasks until all return Ok or one returns Err.
Returns Ok with all values when every Task returns Ok. Stops on the first
Err; remaining running Tasks are aborted. Sequential by default; pass a
concurrency option to run more than one Task at a time.
Pass { collect: false } when only collective success or failure matters.
The returned Task produces void on success and does not store the Ok
values.
With a mapping function, maps input values to Tasks before running them. The
mapper runs immediately when all is called, before the returned Task
starts. Array mappers receive (value, index). Record mappers receive
(value, key). Mapper defects happen at construction time, so keep mappers
pure and cheap.
Similar to Promise.all, but runs Tasks, returns Result values, and aborts remaining Tasks on the first Err.
Example
import {
assertErr,
assertEqual,
assertType,
all,
createRun,
err,
ok,
type Result,
type Task,
type Typed,
} from "@evolu/common";
const savedUserIds: Array<string> = [];
const saveUser =
(id: string): Task<number, SaveUserFailedError> =>
() => {
if (id === "missing") {
return err({ type: "SaveUserFailed", userId: id });
}
savedUserIds.push(id);
return ok(1);
};
interface SaveUserFailedError extends Typed<"SaveUserFailed"> {
readonly userId: string;
}
await using run = createRun();
const saveResult = await run(
all([saveUser("user-1"), saveUser("missing"), saveUser("user-3")], {
collect: false,
}),
);
assertType<Result<void, SaveUserFailedError>, typeof saveResult>();
assertErr(saveResult, {
type: "SaveUserFailed",
userId: "missing",
});
assertEqual(savedUserIds, ["user-1"]);
Call Signature
function all<TTasks>(
tasks: TTasks,
options: AllOptions,
): Task<
void,
InferTaskErr<TTasks[keyof TTasks]>,
ParameterIntersection<
TTasks[keyof TTasks] extends TTask
? TTask extends AnyTask
? (deps: InferTaskDeps<TTask>) => void
: never
: never
>
>;
Defined in: packages/common/src/Task.ts:3036
Runs a Task record without collecting its Ok values.
Call Signature
function all<TTasks>(
tasks: TTasks,
options?: TaskCollectionOptions,
): Task<
InferTasksOk<TTasks>,
InferTaskErr<TTasks[number]>,
ParameterIntersection<
TTasks[number] extends TTask
? TTask extends AnyTask
? (deps: InferTaskDeps<TTask>) => void
: never
: never
>
>;
Defined in: packages/common/src/Task.ts:3078
Runs a Task array and preserves its shape.
Example
import {
assertOk,
assertType,
all,
createRun,
ok,
type Result,
type Task,
} from "@evolu/common";
interface User {
readonly id: string;
}
interface Post {
readonly id: string;
}
const fetchUser: Task<User> = () => ok({ id: "user-1" });
const fetchPosts: Task<ReadonlyArray<Post>> = () => ok([{ id: "post-1" }]);
await using run = createRun();
const dashboard = await run(all([fetchUser, fetchPosts]));
assertType<Result<readonly [User, ReadonlyArray<Post>]>, typeof dashboard>();
assertOk(dashboard, [{ id: "user-1" }, [{ id: "post-1" }]]);
Call Signature
function all<TTasks>(
tasks: TTasks,
options?: TaskCollectionOptions,
): Task<
InferTasksOk<TTasks>,
InferTaskErr<TTasks[keyof TTasks]>,
ParameterIntersection<
TTasks[keyof TTasks] extends TTask
? TTask extends AnyTask
? (deps: InferTaskDeps<TTask>) => void
: never
: never
>
>;
Defined in: packages/common/src/Task.ts:3128
Runs a Task record and preserves its keys.
Example
import {
assertOk,
assertType,
all,
createRun,
ok,
type Result,
type Task,
} from "@evolu/common";
interface User {
readonly id: string;
}
interface Post {
readonly id: string;
}
const fetchUser: Task<User> = () => ok({ id: "user-1" });
const fetchPosts: Task<ReadonlyArray<Post>> = () => ok([{ id: "post-1" }]);
await using run = createRun();
const result = await run(all({ user: fetchUser, posts: fetchPosts }));
assertType<
Result<{ readonly user: User; readonly posts: ReadonlyArray<Post> }>,
typeof result
>();
assertOk(result, {
user: { id: "user-1" },
posts: [{ id: "post-1" }],
});
Call Signature
function all<TValues, TTask>(
values: TValues,
fn: (value: TValues[number], index: number) => TTask,
options: AllOptions,
): Task<
void,
InferTaskErr<TTask>,
ParameterIntersection<
TTask extends TTask
? TTask extends AnyTask
? (deps: InferTaskDeps<TTask>) => void
: never
: never
>
>;
Defined in: packages/common/src/Task.ts:3138
Maps an array to Tasks without collecting their Ok values.
Call Signature
function all<TValues, TTask>(
values: TValues,
fn: (value: TValues[keyof TValues], key: keyof TValues) => TTask,
options: AllOptions,
): Task<
void,
InferTaskErr<TTask>,
ParameterIntersection<
TTask extends TTask
? TTask extends AnyTask
? (deps: InferTaskDeps<TTask>) => void
: never
: never
>
>;
Defined in: packages/common/src/Task.ts:3148
Maps record values to Tasks without collecting their Ok values.
Call Signature
function all<TValues, TTask>(
values: TValues,
fn: (value: TValues[number], index: number) => TTask,
options?: TaskCollectionOptions,
): Task<
{ readonly [K in string | number | symbol]: InferTaskOk<TTask> },
InferTaskErr<TTask>,
ParameterIntersection<
TTask extends TTask
? TTask extends AnyTask
? (deps: InferTaskDeps<TTask>) => void
: never
: never
>
>;
Defined in: packages/common/src/Task.ts:3201
Maps an array to Tasks and collects their Ok values in the same shape.
Example
import {
assertEqual,
assertOk,
assertType,
all,
createRun,
ok,
type Result,
type Task,
} from "@evolu/common";
interface User {
readonly id: string;
}
const loadUser =
(id: string): Task<User> =>
() =>
ok({ id });
const userIds = ["user-1", "user-2"] as const;
const indexes: Array<number> = [];
const loadUsers = all(userIds, (id, index) => {
indexes.push(index);
return loadUser(id);
});
// Mapping is eager: it happens before the returned Task starts.
assertEqual(indexes, [0, 1]);
await using run = createRun();
const result = await run(loadUsers);
assertType<Result<readonly [User, User]>, typeof result>();
assertOk(result, [{ id: "user-1" }, { id: "user-2" }]);
Call Signature
function all<TValues, TTask>(
values: TValues,
fn: (value: TValues[keyof TValues], key: keyof TValues) => TTask,
options?: TaskCollectionOptions,
): Task<
{ readonly [K in string | number | symbol]: InferTaskOk<TTask> },
InferTaskErr<TTask>,
ParameterIntersection<
TTask extends TTask
? TTask extends AnyTask
? (deps: InferTaskDeps<TTask>) => void
: never
: never
>
>;
Defined in: packages/common/src/Task.ts:3265
Maps record values to Tasks and preserves the record's keys.
Example
import {
assertEqual,
assertOk,
assertType,
all,
createRun,
ok,
type Result,
type Task,
} from "@evolu/common";
interface User {
readonly id: string;
}
const loadUser =
(id: string): Task<User> =>
() =>
ok({ id });
const userIdsByRole = {
admin: "user-1",
reviewer: "user-2",
} as const;
const roles: Array<keyof typeof userIdsByRole> = [];
const loadUsersByRole = all(userIdsByRole, (id, role) => {
roles.push(role);
return loadUser(id);
});
// Mapping is eager: it happens before the returned Task starts.
assertEqual(roles, ["admin", "reviewer"]);
await using run = createRun();
const result = await run(loadUsersByRole);
assertType<
Result<{ readonly admin: User; readonly reviewer: User }>,
typeof result
>();
assertOk(result, {
admin: { id: "user-1" },
reviewer: { id: "user-2" },
});