There are times when I encounter a scenario where I must perform multiple operations in sequence. If each operation relies solely on data from the previous step, then it's simple with something like
pipe(startingData, TE.chain(op1), TE.chain(op2), TE.chain(op3), ...)
. However, it becomes complex when op2
also requires data from startingData
, leading to nested callbacks.
Is there a better way to structure the code without creating a pyramid of callbacks as shown below?
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 }))
)
)
)
)
);