Effect CommunityEC
Effect Community3y ago
12 replies
erikshestopal

Proper way of handling a specific case in effect-ts

Hey all, new to the community and been playing around with effect-ts and loving it so far. I'm still learning some of the patterns and could use some assitance.

Given the code below, what is the "proper" way of handling the conditional case that I have outlined in the comments?

const program = Effect.gen(function* ($) {
  const data: DataModel[] = yield* $(Effect.succeed(expected));
  const cached: Option.Option<DataModel[]> = yield* $(cache(data));

  // Start
  //Better way to handle this option case and early return?
  const models = pipe(
    cached,
    Option.getOrElse(() => []),
  ) satisfies DataModel[];

  if (models.length === 0) {
    return Effect.unit();
  }
  // end

  return yield* $(
    Effect.all(DatabaseTag, SchemaTag),
    Effect.flatMap(([database, schema]) => interpreter({ db: database, modelToSchemaMap: schema }, models)),
  );
});
Was this page helpful?