我经常遇到这种情况,需要完成几个顺序操作。如果每个操作都专门使用上一步中的数据,那么我可以很高兴地做类似pipe(startingData, TE.chain(op1), TE.chain(op2), TE.chain(op3), ...)
. op2
当还需要来自 的数据时,我找不到一个很好的方法来编写这个startingData
,没有一堆嵌套回调。
在下面的示例中,如何避免厄运金字塔?
declare const op1: (x: {one: string}) => TE.TaskEither<Error, string>;
declare const op2: (x: {one: string, two: string}) => TE.TaskEither<Error, string>;
declare const op3: (x: {one: string, two: string, three: string}) => TE.TaskEither<Error, string>;
pipe(
TE.of<Error, string>('one'),
TE.chain((one) =>
pipe(
op1({ one }),
TE.chain((two) =>
pipe(
op2({ one, two }),
TE.chain((three) => op3({ one, two, three }))
)
)
)
)
);