Compose multiple Results with Generators
mainUse Result.gen to chain multiple Result operations using a generator function. This avoids nested callbacks and early returns. Use yield* to unwrap a Result or short-circuit on error. For asynchronous operations, use Result.gen with an async function* and yield* Result.await(promise).
// Synchronous composition
const result = Result.gen(function* () {
const a = yield* parseNumber(inputA); // Unwraps or short-circuits
const b = yield* parseNumber(inputB);
const c = yield* divide(a, b);
return Result.ok(c);
});
// Async composition
const result = await Result.gen(async function* () {
const user = yield* Result.await(fetchUser(id));
const posts = yield* Result.await(fetchPosts(user.id));
return Result.ok({ user, posts });
});