Javascript 将fp ts任务链接到右侧的任意一个

Javascript 将fp ts任务链接到右侧的任意一个,javascript,typescript,functional-programming,fp-ts,Javascript,Typescript,Functional Programming,Fp Ts,我有一个2个嵌套请求流,其中可能有3个不同的结果: 其中一个请求返回错误 用户不是匿名的,返回配置文件 用户是匿名的,返回false 这两个请求都可能引发错误,因为这两个请求都实现了task const isAuth = ():TE.TaskEither<Error, E.Either<true, false>> => TE.tryCatch(() => Promise(...), E.toError) const getProfile = ():T

我有一个2个嵌套请求流,其中可能有3个不同的结果:

  • 其中一个请求返回错误
  • 用户不是匿名的,返回配置文件
  • 用户是匿名的,返回false
  • 这两个请求都可能引发错误,因为这两个请求都实现了
    task

    const isAuth = ():TE.TaskEither<Error, E.Either<true, false>>  
       => TE.tryCatch(() => Promise(...), E.toError)
    const getProfile = ():TE.TaskEither<Error, Profile>  
       => TE.tryCatch(() => Promise(...), E.toError)
    
    但是作为回报,我得到了
    E.other
    ,这不方便,因为我必须手动从
    错误中提取
    匿名
    状态


    如何解决这个问题?

    不知道您是否过度简化了实际的代码,但是
    E。或者
    同构于
    布尔值
    ,所以让我们继续使用更简单的东西

    declare const isAuth: () => TE.TaskEither<Error, boolean>;
    declare const getProfile: () => TE.TaskEither<Error, Profile>;
    
    此表达式的类型为
    task或
    。您可能需要添加一些类型注释,以便正确地进行类型检查,因为我自己还没有运行代码

    编辑:

    您可能需要将lambda提取为命名函数以获得正确的类型,如下所示:

    const tryGetProfile: (authed: boolean) => TE.TaskEither<Error, E.Either<false, Profile>> = authed
      ? pipe(getProfile(), TE.map(E.right))
      : TE.right(E.left(false));
    
    const result: TE.TaskEither<Error, E.Either<false, Profile>> = pipe(
      isAuth(),
      TE.chain(tryGetProfile)
    );
    
    const tryGetProfile:(authed:boolean)=>TE.taskOrther=authed
    ? 管道(getProfile(),TE.map(E.right))
    :TE.right(E.left(false));
    const result:TE.taskator=pipe(
    isAuth(),
    TE.链(tryGetProfile)
    );
    
    E.other
    没有多大意义,因为类型是
    other
    ,因此无论如何都无法获得
    other
    。您丢失
    other
    层的原因是自然转换
    TE。从
    other
    Task
    您作为构图的第一步执行。哎呀,刚才注意到您的初始other具有文本作为类型参数,因此是
    other
    。问题仍然是一样的。@bob是的,我使用自然转换使其工作,因为我没有找到一种方法来编写正确的管道,它将返回
    E.
    选项
    ,这不是重点,在右边部分,并将错误从第二个请求放到左边部分-这就是我的问题所在。谢谢!但是如何获得正确的类型呢?我尝试了这个
    A.sequenceT(TE.tasketherseq)(result)
    ,但作为回报,我得到了数组类型
    TE.tasktory
    ,这不是我想要的:(你知道怎么解决吗?@IvanTarasov不太确定,可能是有错误或打字错误。我用一些打字建议更新了答案,你可以从那里开始工作,它应该会给你一些合理的编译器错误。
    declare const isAuth: () => TE.TaskEither<Error, boolean>;
    declare const getProfile: () => TE.TaskEither<Error, Profile>;
    
    pipe(
      isAuth(),
      TE.chain(authed => authed 
        ? pipe(getProfile(), TE.map(E.right)) // wrap the returned value of `getProfile` in `Either` inside the `TaskEither`
        : TE.right(E.left(false))
      )
    )
    
    const tryGetProfile: (authed: boolean) => TE.TaskEither<Error, E.Either<false, Profile>> = authed
      ? pipe(getProfile(), TE.map(E.right))
      : TE.right(E.left(false));
    
    const result: TE.TaskEither<Error, E.Either<false, Profile>> = pipe(
      isAuth(),
      TE.chain(tryGetProfile)
    );