Im working with fp-ts library. My function runs in paralel 2 other functions (lets call them first and second) that can either fail or succeed. They both return something like TE.TaskEither<Error, User>
So my function calls them pretty straighforward
TE.sequenceArray([first, second])
This function return type is TE.TaskEither<Error, ReadonlyArray<User>>
Now, the behvior is suppoused to be like this:
case errors, succeed:
of [err, err], _ -> left(user does not exist)
of _, [el] -> right(el)
of _, _ -> left(internal server error)
Meaning if any of users is found, the function should return ReadonlyArray<User>> with either 2 or 1 users, and not throw error. If there are 2 errors thrown meaning no user is found it should also return Error wrapped in TaskEither.
Sorry if my explenation is a bit blurry im still pretty new to fp-ts so please be kind to me:)
Ive tried to use TE.fold and TE.chain but the issue is that i can't find the way to ignore error when one of calls return it
I tried:
F.pipe(
TE.sequenceArray([fist, second]),
TE.chain(successes => {
if (successes.length > 0) {
return TE.right(successes);
} else {
return TE.left(Error());
}
}),
TE.map(results => results.flat()),
);
This solution seams to ignore my if statement