如何使用sequenceT和readertaskeeither进行不同错误类型的两个并行请求?



我想使用sequenceT函数进行两个并行请求并处理结果,但它向我显示了一个我无法自行解决的错误

import * as RTE from 'fp-ts/ReaderTaskEither'
import * as Ap from 'fp-ts/Apply'
Ap.sequenceT(RTE.ApplyPar)(
getUserById('1'),
getSpecialistById('2')
) // <- This method shows error

误差

Type 'SpecialistNotFoundError' is not assignable to type 'UserNotFoundError'.
Types of property 'code' are incompatible.
Type '"SPECIALIST_NOT_FOUND"' is not assignable to type '"USER_NOT_FOUND"'

可能有助于理解问题的代码

function getSpecialistById (specialistId: string): RTE.ReaderTaskEither<PrismaClient, SpecialistNotFoundError, Specialist> {
return (prisma: PrismaClient) => {
return TE.tryCatch(
() => prisma.specialist.findUnique({ where: { id: specialistId }, rejectOnNotFound: true }),
() => new SpecialistNotFoundError()
)
}
}
function getUserById (userId: string): RTE.ReaderTaskEither<PrismaClient, UserNotFoundError, User> {
return (prisma: PrismaClient) => {
return TE.tryCatch(
() => prisma.user.findUnique({ where: { id: userId }, rejectOnNotFound: true }),
() => new UserNotFoundError()
)
}
}
class UserNotFoundError extends Error {
readonly code = 'USER_NOT_FOUND'
constructor () {
super('User not found')
}
}
class SpecialistNotFoundError extends Error {
readonly code = 'SPECIALIST_NOT_FOUND'
constructor () {
super('Specialist not found')
}
}

我认为,而不是使用sequenceT(因为我不认为它能够正确处理你想做的类型),我会使用Do符号,如下所示:

const result = pipe(
RTE.Do,
RTE.apS("user", getUserById("1")),
RTE.apSW("spec", getSpecialistById("2")),
RTE.map(({ user, spec }) => {
return `${user.id}:${spec.id}`;
})
);

result在本例中将是RTE.ReadTaskEither<PrismaClient, UserNotFoundError | SpecialistNotFoundError, string>,因为我在map中返回了string

这里是Do符号的文档。注意在底部,它显示了如何并行执行

相关内容

  • 没有找到相关文章

最新更新