2

这里解释了面向铁路的编程(ROP):

https://fsharpforfunandprofit.com/rop/

有没有办法使用这种模式Fluture

我可以像这样使用这两个辅助方法进行 ROP:

const bind = f => x => Future.attempt(() => f(x));
const bindAsync = f => x => Future.tryP(() => f(x));

Future.of("TEST")
    .chain(bind(doThis))
    .chain(bind(doThat))
    .chain(bindAsync(doThisAsync))
    .chain(bindAsync(doThatAsync))
    .chain(bind(doAnotherThing))
    .chain(bindAsync(doAnotherThingAsync))
    .
    .
    .

有没有更好的方法来删除bindbindAsync并自动进行绑定?

4

1 回答 1

1

我不建议像这样构建您的程序。看起来你有抛出异常和使用承诺(这是你想通过使用 ROP 摆脱的两件事)分布在整个程序中的函数,然后在顶层组合它们。

相反,您应该包装您正在使用的库,以便摆脱承诺/异常并将它们转换为尽可能接近问题库的期货。例如,如果您使用 Promise 进行 HTTP 网络调用,请包装您的网络库,以便它返回期货。

这意味着您可以将您的组合函数更改为对纯函数和 Future-returning 函数进行操作,这些函数可以直接使用mapand组合chain

于 2019-11-08T15:16:35.343 回答