Conditionally Execute an Effect in a Pipe Using `Effect.when`

I am trying to execute a certain operation based on a condition, I would like this to be reflected in the control flow but I'm not sure how Effect.when can be used in the pipe
const flag = true;

myEffectFunction().pipe(
  Effect.map(output => { output }),
  Effect.tap(output => anotherFunction(output)), // Should be executed only when flag === true
  Effect.tapError(error => logError(error)),
  Effect.catchIf(
    error => error instanceof MyErrorClass,
    () => Effect.succeed,
  ),
)

This could be done with
const flag = true;

myEffectFunction().pipe(
  Effect.map(output => { output }),
  Effect.tap(output => flag && anotherFunction(output)), // <<-- anotherFunction is called only when flag === true
  Effect.tapError(error => logError(error)),
  Effect.catchIf(
    error => error instanceof MyErrorClass,
    () => Effect.succeed,
  ),
)

But when scanning vertically the pipe, I do not see that the Effect.tap is conditioned by flag === true

If I add Effect.when(() => flag) in the main pipe, it seems the rest does not necessarily run.
Was this page helpful?