[SOLVED] merging 2 TaskEither in fp-ts

Issue

I am working on a project that using fp-ts

I have 2 TaskEither object like TaskEither<ErrorA, A>, TaskEither<ErrorB, B>

I wanted to merging this objects contents and create new TaskEither<ErrorA, A>,
Example A object = {a: 123, b: 456, c: 0}

Example B object = {c: 789}

I wanted to create newObject: TaskEither<ErrorA, A>

and If everything goes well, expecting value should be {a: 123, b: 456, c: 789 }

Solution

Using TE.Do is of course a solution (clean and small).
For another usage, you can just chain your tasks usings pipes.

export const a = (): TE.TaskEither<ErrorA, A> => {
  return TE.right({ a: 123, b: 456, c: 0 });
}

export const b = (): TE.TaskEither<ErrorB, B> => {
  return TE.right({ c: 789 });
}

export const c =(): TE.TaskEither<ErrorA, A> => {
  return pipe(
    a(),
    TE.chain(a => {
      return pipe(
        b(),
        TE.map(b => {
          return {
            ...a,
            c: b.c,
          }
        }),
      );
    }),
  );
}

Answered By – zenbeni

Answer Checked By – Senaida (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *