Ok, so I have a series of calls to functions that return Akka Futures, and I'm chaining them by using flatMap and map like so:
doAsyncCall(..).flatMap { res1 =>
doAsyncCall2(..).flatMap { res2 =>
doAsyncCall3(..).map { res3 =>
res
}
}
}
which can be spread out across different function calls. Now each of these doAsyncCallX calls returns a Future[Result], and I would like to be able to compose these results, passing them over on the chain until in the end I can return the composed result of all the results in the chain.
To do this, I thought of having each doAsyncCallX receive an implicit parameter of the previous result and combine the obtained result with the previous one. The problem is that I cannot see a way of doing this without littering the usage of my library with implicit vals like this:
doAsyncCall(..).flatMap { res1 =>
implicit val implicitRes1 = res1
doAsyncCall2(..).flatMap { res2 =>
implicit val implicitRes2 = res2
doAsyncCall3(..).map { res3 =>
res
}
}
}
What I would like to do is be able to intercept the application of flatMap to make sure that the previous response is passed correctly as the implicit parameter of the next doAsyncCallX call. Any idea on how to accomplish this?
Perhaps I'm missing something, but is there a reason to use implicits at all?
If you need to return a result composing multiple of the intermediate results, change the yield to yield something different, perhaps a tuple, like
yield((res1,res2,res3))
. The result of which would be something likeFuture[(Result1,Result2,Result3)]