1

Given the following bits:

OnRequestSuccess is a async function that does some work and returns a string OnRequestFailure is a async function that does some work and returns a ERROR

This is somewhat the desiredFlow. I reckon I could make the fold call the asyncMethods, but then I am left with TaskEither<Promise,Promise> and I just want the final types.


pipe(input,chain(asyncRequestChatMessage), map(asyncOnRequestSuccess), mapLeft(asyncOnRequestFail),foldW(toTask,toTask), finalCall)

By the end of the chain I want to grab whatever was outputed from either (so a Task) and call a another async function with it.

My problem is that since both OnError and OnRequest are async, and I cant seem to make them resolve inside the flow. How do we do this?

Example code:


    const success = (result: string) => TE.tryCatch(() => onRequestSuccess(result), E.toError);
    const fail = (result: Error) => TE.tryCatch(() => onRequestFail(result), E.toError);


 // This is the problem

    const result = pipe(input, TE.right, TE.chain(requestChatMessage), TE.map(success), TE.mapLeft(fail), TE.flattenW);



The FlattenW solves the right side, but there isnt a flattenLeftW and I cant get orElse to work either.

In the example above how can I get by the end of it either a TASK of both types (onRequestSuccess and OnRequestFail) that I can then chain into another call?

Or how can I flatten the Left side of the TaskEither, since right now this returns

const result: TE.TaskEither<Error | TE.TaskEither<Error, Error>, string>

It feels I am missing something here really obvious

Morphex
  • 306
  • 3
  • 15
  • Not sure what you are exactly trying to do. `Either` represents a synchronous computation that can be a `Left` or `Right` value . `Task` represents a computation that will be solved in a future (similar to a `Promise` but "never" fails). `TaskEither` represents an asynchronous computation that once it's solved the result is of type `Either` (it's the same as a `Task>`). You have different ways of chaining operations. It's not clear for me how your operations work. Could you describe at a high level what you need? Looks like you are trying to make it work by force – Gastón Schabas Aug 09 '23 at 03:12
  • The [suffix `W` means `Widen`](https://gcanti.github.io/fp-ts/guides/code-conventions.html#what-a-w-suffix-means-eg-chainw-or-chaineitherkw). It is used when you chained different types of `TaskEither.left`. – Gastón Schabas Aug 09 '23 at 03:15
  • Ok, I will try to explain a bit better. I am trying to build a "flow/pipeline" of operations that I want to execute in sequence, but by the end of the sequence of TE, I want to execute the same async function in both the left/right and return a single value out of it. I cant seem to get this to work, but I am probably doing something wrong here. – Morphex Aug 10 '23 at 13:56
  • What do you mean exactly with "**want to execute the same async function in both the left/right**"? [either](https://gcanti.github.io/fp-ts/modules/Either.ts.html) represents an operation that will have a result of type right or left. once the either operation is processed, you have a result of left or right. – Gastón Schabas Aug 11 '23 at 01:22

0 Answers0