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