这里解释了面向铁路的编程(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))
.
.
.有没有更好的方法来自动删除bind,bindAsync和绑定?
发布于 2019-11-08 23:16:35
我不建议这样组织你的程序。看起来你有一些抛出异常和使用promises的函数(这是你想通过使用ROP去掉的两个东西),然后在顶层组合它们。
相反,您应该包装您正在使用的库,以便您摆脱promises / exceptions,并将它们转换为尽可能接近问题库的未来。例如,如果您使用promises进行HTTP网络调用,请包装您的网络库,以便它返回未来。
这意味着您可以将组合函数更改为对纯函数和未来返回函数进行操作,这些函数可以直接使用map和chain组合。
https://stackoverflow.com/questions/58768361
复制相似问题