0

I am new in fp-ts so please help me resolve my problem: I need to log the same error multiple times on different levels using asynchronous function. Here is my example code:

const myProgram = pipe(
    tryCatch(() => someAsyncFunc(), toError),
    mapLeft(async (err1) => {
        await loggerAsyncFunc();
        return err1;
    }),
)

const main = pipe(
    myProgram,
    mapLeft((err2) => {
        // err2 is a pending promise :(
    })
)();

I am using mapLeft to do that but it doesn't work. What I need to do to have in err2 the value of the error (err1) instead of pending promise?

embe
  • 43
  • 1
  • 1
  • 3
  • You didn't provide types so I can only guess that `tryCatch` returns an `Either`. Since an async function returns a `Promises` you probbaly have a `Either, R>`, that is, you need to compose `mapLeft` with the `map` instance of the `Promise` type, which FP-TS hopefully provides. –  May 28 '20 at 18:18

1 Answers1

2

Assuming you are using TaskEither, orElse can chain the Task on the left side.

const myProgram = pipe(
    TE.tryCatch(() => someAsyncFunc(), toError),
    // orElse is like a chain on the left side
    TE.orElse(err1 => pipe(
        TE.rightTask(longerAsyncFunc),
        TE.chain(() => TE.left(err1))
    )),
);

const main = pipe(
    myProgram,
    mapLeft((err2) => {
        // err2 is no longer a promise
    })
)();
Dan Fey
  • 56
  • 1
  • 4