简体   繁体   English

如何使用 fp-ts 将 TaskOption 转换为 TaskEither?

[英]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 :我有一个函数可用于在数据库中查找对象并返回一个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 .在我的register实现中,我想检查我尝试保存的Schema是否存在(我想使用find ),但我不知道如何将TaskOption映射到TaskEither I tried to do this:我试图这样做:

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

but TE.fromTaskOption doesn't return the TaskEither I expect.TE.fromTaskOption没有返回我期望的TaskEither I also tried TE.fromTaskOptionK but it didn't work either, it returns a weird shaped function instead.我也试过TE.fromTaskOptionK但它也不起作用,它返回一个奇怪的形状函数。

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).我的用例是,如果TaskOptionnone我会尝试保存(这意味着架构尚未注册),否则我将返回错误(架构已注册)。 How can I solve this?我该如何解决这个问题?

TE.fromTaskOption requires a value to put into Left , when the TO is None当 TO 为None时, TE.fromTaskOption需要一个值放入Left

Try this:尝试这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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