Effect CommunityEC
Effect Community3y ago
78 replies
Sadra

Avoiding Additional Variables in Pipe Scope for Readability

Does anyone have time to provide suggestions on avoiding the use of additional variables in the scope when using pipes, while still preserving readability?

  let adminId: number;
  let input: JsonRequest;

  const res = await Effect.unit.pipe(
    Effect.flatMap(() => validateInput(request)),
    Effect.tap((x) => Effect.sync(() => (input = x))),
    Effect.flatMap(() => authenticateAdmin(input)),
    Effect.tap((res) => Effect.sync(() => (adminId = res))),
    Effect.flatMap(() => validateTitle(input)),
    Effect.flatMap(() => checkSkips(input)),
    Effect.flatMap(() => addSkip(input, adminId)),
    Effect.map(() => ({ ok: true as const, err: null })),
    Effect.catchAll((err) =>
      Effect.succeed({ ok: false as const, err: err._tag }),
    ),
    Effect.runPromise,
  );
Was this page helpful?