我有 2 个嵌套请求的流程,其中可能是 3 个不同的结果:
- 请求之一返回错误
- 用户不是匿名的,返回 Profile
- 用户是匿名的,返回 false
这两个请求都可能引发错误,并且由于该实现TaskEither
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.Either<Error, E.Either<false, Profile>>
我试图这样做:
pipe(
isAuth()
TE.chain(item => pipe(
TE.fromEither(item),
TE.mapLeft(() => Error('Anonimous')),
TE.chain(getProfile)
))
)
但作为回报,我得到了E.Either<Error, Profile>
,女巫不方便,因为我必须Anonymous
手动从Error
.
如何解决这个问题?