0

I'm using fp-ts to fetch two data sets and then pass that to a single function (or handle errors). This function works, but I have to use .then() or await to get it to work.

How can I make this "more functional" by using some native fp-ts functions, so that I can have everything in one nice pipe() function, but get the same result?

export default async function CalendarRoute() {
  return pipe(
    TE.Do, // Start the computation
    TE.bind("events", () => getEvents),
    TE.bind("route", () => getRoute("kalender")),
  )().then(matchEither(renderError, ({ events, route }) => renderCalendarPage(events, route)));
}

The getter functions have return types TaskEither<Error, MyType>.

eivindml
  • 2,197
  • 7
  • 36
  • 68
  • https://stackoverflow.com/questions/59526500/fp-ts-using-async-function-in-middle-of-pipe https://stackoverflow.com/questions/68992182/fp-ts-how-to-handle-async-operations-within-pipe https://stackoverflow.com/questions/58560310/chain-some-async-tasks-in-fp-ts-retaining-every-tasks-result – Bergi Jun 26 '23 at 19:56
  • Does this answer your question? [Running an array of TaskEithers in parallel, but continue if 1 or more task fails](https://stackoverflow.com/questions/60471399/running-an-array-of-taskeithers-in-parallel-but-continue-if-1-or-more-task-fail) – Gastón Schabas Jun 26 '23 at 20:35

1 Answers1

1

Instead of executing the TaskEither and then folding/matching, you can do your fold/match within the pipe itself.

export default async function CalendarRoute() {
  return pipe(
    TE.Do, // Start the computation
    TE.bind("events", () => getEvents),
    TE.bind("route", () => getRoute("kalender")),
    TE.matchW(
      renderError,
      ({ events, route }) => renderCalendarPage(events, route)
    )
  )();
}
cdimitroulas
  • 2,380
  • 1
  • 15
  • 22