0

我是 fp-ts 的新手,所以请帮我解决我的问题:我需要使用异步函数在不同级别上多次记录相同的错误。这是我的示例代码:

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

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

我习惯mapLeft这样做,但它不起作用。我需要做什么才能获得err2错误(err1)的价值而不是待定的承诺?

4

1 回答 1

1

假设您正在使用TaskEither,orElse 可以将Task 链接在左侧。

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
    })
)();
于 2020-09-16T16:46:57.543 回答