简体   繁体   中英

How to turn a TaskOption into a TaskEither using fp-ts?

I have a function that can be used to find an object in the database and returns a TaskOption :

find: (key: SchemaInfo) => TO.TaskOption<Schema>

and another one that saves it:

register: (schema: Schema) => TE.TaskEither<Error, void>

In my register implementation I'd like to check whether the Schema I'm trying to save exists or not (I want to use find ), but I don't know how to map a TaskOption into a TaskEither . I tried to do this:

pipe(
    findSchema(schema.info),
    TE.fromTaskOption,
    TE.swap
)

but TE.fromTaskOption doesn't return the TaskEither I expect. I also tried TE.fromTaskOptionK but it didn't work either, it returns a weird shaped function instead.

My use case is that in case the TaskOption is none I try saving (this means that the schema is not registered yet) otherwise I return an error (the schema is already registered). How can I solve this?

TE.fromTaskOption requires a value to put into Left , when the TO is None

Try this:

pipe(
    findSchema(schema.info),
    TE.fromTaskOption(() => "No schema"),
    TE.swap
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM