0

I'm a freshman in fp-ts, trying to understand how to compose together some things.

So, I have for example TaskEither, which are doing fetch and based on the result from a fetch I want to run another fetch or jump to next step, currently, I've done this in such way. Maybe someone knows a better way how to do this?


pipe(
  tryCatch(
    () => fetch('https://example.com'),
    reason => String(reason)
  ),
  chain(
    d => d ?
    tryCatch(
      () => fetch('https://example.com'),
      reason => String(reason)
    ) : right({})
  ),
  chain(
    finalResult => {
      //console.log(finalResult)
      return right(finalResult)
    }
  )
)

Thanks for reading:)

Dmytro Filipenko
  • 861
  • 1
  • 9
  • 25
  • The `pipe` combinator seems to be variadic (variable no. of arguments). `pipe` is a variant of function composition and there is no way of short circuiting it. To allow for one with short circuit semantics you probably need kleisli composition, i.e. a variadic combinator similar to `pipe` that composes monadic actions, i.e. functions that return a monadic type. I don't know if there is such a combinator in fp-ts though. –  Sep 19 '19 at 10:48
  • @Dmytro `d` in your example would be a `boolean`? – Giovanni Gonzaga Sep 23 '19 at 12:07
  • @GiovanniGonzaga is it change something if it Boolean or a different object? – Dmytro Filipenko Sep 25 '19 at 04:35
  • 1
    @DmytroFilipenko I was asking because there are usually combinators defined in terms of a Applicative or Monad like `if` or `when` that could help in this regard. In any case, I don't see anything wrong with your code here. A slightly more compact approach could be to use a `fold` over `boolean` instead of the built-in ternary syntax for booleans to pick the correct task with which to continue. See the function I'm adding here: https://github.com/gcanti/fp-ts/pull/1013 – Giovanni Gonzaga Nov 14 '19 at 08:43

0 Answers0