This solution and 2 above it are, in fact, the same. They offer a simple flatMaps composition response. This is useful for one-time solutions.
for { oUuid <- getOneRecordByModel(x) oFlight <- oUuid.map(getRecordByUUID).getOrElse(Future.successful(None)) } yield oFlight
I suspect that I gave the method a signature, you are going to use this strategy a lot. If so, @Eugene Zhulenev answer above (which is a more functional solution). The thought of Monad Transformers may look a little intimidating at first glance, a piece of code here:
val flightByUUID = for { flightByDetailModel <- optionT(getOneRecordByModel(x)) flightByUUIDModel <- optionT(getRecordByUUID(flightByDetailModel)) } yield flightByUUIDModel flightByUUID.run
Very simple and scalable when you start adding complexity. Hope this helps you.
jordan3
source share