Fp-ts: How to convert Either<Error, Task<any>> into TaskEither<Error, any>

Created on 2 Jan 2020  路  3Comments  路  Source: gcanti/fp-ts

I have a pipe that return Either<Error, Task<any>> but what I need is TaskEither<Error, any>.

How can I convert Either<Error, Task<any>> to TaskEither<Error, any>?

Is there any helper any utility function for doing this?

Most helpful comment

That's sequence

declare const a: E.Either<Error, T.Task<unknown>>;

const b: TE.TaskEither<Error, unknown> = E.either.sequence(T.task)(a);

All 3 comments

That's sequence

declare const a: E.Either<Error, T.Task<unknown>>;

const b: TE.TaskEither<Error, unknown> = E.either.sequence(T.task)(a);

@giogonzo When I copy paste the sequence example above and leave out the explicit type of b, its type is inferred as Task<Either<Error, unknown>>.

So, is Task<Either<Error, unknown>> intended to be and stay the same as TaskEither<Error, unknown> in the API?

@ford04 I omitted the part "given how a TaskEither is represented" in my previous answer.

It is definitely intended to be this way today, and for what I can tell also intended to stay the same.

This is actually an advantage of the current class-less / naked datatype encoding.
Similarly, you can do things like

// collect all successes AND failures:
A.array.sequence(T.task)(arrayOfTaskEithers)

// fail if some failure is encountered:
A.array.sequence(TE.taskEither)(arrayOfTaskEithers)

In any case, I don't see this as a major concern: with proper type annotations in place, if this ever changes, you'll be able to fix the error by providing a different implementation for such transformation

Was this page helpful?
0 / 5 - 0 ratings

Related issues

maciejsikora picture maciejsikora  路  3Comments

FredericLatour picture FredericLatour  路  3Comments

mmkal picture mmkal  路  3Comments

gcanti picture gcanti  路  3Comments

steida picture steida  路  4Comments