Reputation: 715
Railway Oriented Programming(ROP) is explained here:
https://fsharpforfunandprofit.com/rop/
Is there any way to use this pattern with Fluture
I can do ROP with these two helper methods like this:
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))
.
.
.
Is there a better way to remove bind
, bindAsync
and do binding automatically?
Upvotes: 2
Views: 243
Reputation: 10884
I wouldn't recommend structuring your program like this. It looks like you have functions that throw exceptions and use promises (which are the two things you want to get rid of by using ROP) spread out over your entire program, then compose these at the top level.
Instead you should wrap the libraries you're using so that you get rid of the promises / exceptions and convert them to futures as close to the problem library as possible. E.g. if you're using promises for doing HTTP network calls, wrap your network library so that it returns futures.
This means you can then change your composition functions to operating on pure functions and Future-returning functions, which can be composed directly using map
and chain
.
Upvotes: 2