How can I "fix" this response, I wanted Future[Option[F4]]
val f4Response: Future[Option[Future[Option[Future[F4]]]]] =
for {
f1Opt <- api.getF1() // Future[Option[F1]]
f2Opt <- if (f1Opt.isDefined) api.getF2(f1Opt.get.id) else Future.successful(None) // getF2 is Future[Option[F3]]
} yield {
for {
f1 <- f1Opt
f2 <- f2Opt
} yield {
for {
f3Opt <- api.getF3(f1.id, f2.id) // Future[Option[F3]]
} yield {
for {
f3 <- f3Opt
} yield {
api.insertF(f1, f2, f3) // Future[Option[F4]]
}
}
}
}
Update
I'm trying to use scalaz but I am getting an error:
val result: Future[Option[f4]] = (
for {
f1 <- OptionT(api.getF1(..))
f2 <- OptionT(api.getF2(..))
f3 <- OptionT(api.getF3(f1.id, f2.id)
} yield api.getF4(f1, f2, f3)
).run
Error is:
[error] found : scala.concurrent.Future[Option[scala.concurrent.Future[F4]]]
[error] required: scala.concurrent.Future[Option[F4]]
Also, I can't access f1.id and f2.id in the line:
f3 <- OptionT(api.getF3(f1.id, f2.id)