The code below doesn't compile and I don't know how to fix it.
def doAsync(n: Int) = Action {
import scala.concurrent.ExecutionContext.Implicits.global
Async {
val f1 = Future.successful(n)
f1.map(x => x match {
case 10 => Ok("first")
case _ => {
val f2 = Future.successful(n)
f2.map(y => Ok("second"))
}
})
}
}
The line f2.map(y => Ok("second"))
produces the compile error:
type mismatch; found: scala.concurrent.Future[play.api.mvc.SimpleResult[String]] required: play.api.mvc.Result
The core "business logic" is: f2
only must run if n is not 10
.
f1 is going to be a future of a result.
You're returning a result for case 10, and a a future of a result otherwise. You need to put the result into the same terms. Try:
f1 flatMap {x => x match {
case 10 => Future.successful(Ok("first"))
case _ => {
val f2 = Future.successful(n)
f2.map(y => Ok("second"))
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With