繁体   English   中英

fp-ts 在 mapLeft 内调用异步 function

[英]fp-ts call async function inside mapLeft

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

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) 而不是挂起的 promise?

假设您正在使用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
    })
)();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM