Simplifying Effect Program with UserId Validation and Logging

Contrived example aside, how can I write this more clearly/concisely?

import { Effect, Console, Brand, Fiber } from "effect";

type UserId = number & Brand.Brand<"UserId">;

const UserId = Brand.refined<UserId>(
  (id) => id > 0,
  (id) => Brand.error(`Expected given ID of ${id} to be positive`)
);

const action = Effect.sleep("1 second").pipe(
  Effect.andThen(() => Effect.sync(() => Math.random() > 0.7))
);

const program = Effect.gen(function*() {
  yield* Effect.addFinalizer(() => Console.log("Shutting down."));

  yield* Console.log("Application started!");

  const fiber = yield* Effect.Do.pipe(
    Effect.andThen(() => {
      return Effect.repeat(action, {
        until: (value) => {
          if (!value) console.log("Retrying...");
          return value;
        }
      })
    }),
    Effect.andThen(() => {
      const userId = UserId(123);
      console.log(`User ID: ${userId}`);
    }),
    Effect.fork
  );

  return yield* Fiber.join(fiber);
});

Effect.runPromise(Effect.scoped(program)).catch(console.error);
Was this page helpful?