Nested Effects (from promises)

Hi all
I am new to effect.ts and trying to figure out how to two possible paths following a promise in an idiomatic way. My goal would be to have a pipeline of first a fetch request and then split the result on the Response.ok boolean (true => Success; false => Failure). Then based on the outcome, I want the Response.json() in the success case, and the Response.text() in a failure case. I would expect it to have the type of Effect.Effect<unknown, string, never> at the end.

This is my current draft, which does result in Effect.Effect<Effect.Effect<unknown, never, never>, Effect.Effect<never, string, never>, never>:

const program = pipe(
  Effect.promise(() => fetch("some-url")),
  Effect.filterOrFail(
    (response) => response.ok,
    Function.identity,
  ),
  Effect.mapBoth({
    onSuccess: (response) => Effect.promise(() => response.json() as Promise<unknown>),
    onFailure: (response) => Effect.promise(() => response.text()),
  }),
);


Is there a eleganter way to achieve this? Or are there some helper functions in the library which unwrap this in the way I would like it? Thank you for any help or explanation.
Was this page helpful?