Effect CommunityEC
Effect Community3y ago
3 replies
fea

Using Effects in a Callback Function: Stumped on How to Yield Effects

Currently using effect in a semi-serious application for the first time and I've reached a point where I'm genuinely stumped.
I've got an external function foo that expects a parameter/callback of type (bar: Bar) => void and I want to use Effects in this callback. I'm calling foo inside an Effect.gen generator, so I'm assuming it's possible somehow to use effects here, but I can't figure out how to yield* effects inside the callback without changing the type of the callback function to an Effect itself (which I can't do, because I don't control the type that foo expects).
I ended up writing this code, which simply runs the effect that I want to use inside the callback, but I feel like there has to be some better way?
Effect.gen(function* (_) {
  const tc = yield* _(TypeChecker);
  const nonEffectfulProcessJSDocComment = (
    pb: PhrasingContentBuilder,
    comment: string | ts.NodeArray<ts.JSDocComment>
  ) =>
    Effect.runSync(
      processJSDocComment(pb, comment).pipe(
        Effect.provideService(TypeChecker, tc),
      )
    );
  builder.paragraph({}, (pb) => {
    nonEffectfulProcessJSDocComment(pb, tag.comment);
  });
});

(Note: processJSDocComment returns an Effect)
Was this page helpful?