在下面的一段代码中,我试图返回Async结果,在这种情况下是Akka.future。当我尝试映射方法响应的结果时,我得到一个编译错误,上面写着
[error] Test.scala:180: type mismatch;
[error] found : scala.concurrent.Future[play.api.mvc.SimpleResult[String]]
[error] required: play.api.mvc.Result
[error] jsonResponse.map((s: String) => Ok(s))
以下是我的尝试:
def testAkka(jsonList: List[String]) = Action {
Async {
val ftrList: List[Future[String]] = jsonList.map((s: String) => Akka.future {returnSomeVal(s)} )
val futureList: Future[List[String]] = Future.sequence(ftrList)
val jsonResponse: Future[String] = futureList.map((f: List[String]) => f.mkString(","))
Akka.future {
jsonResponse.map((s: String) => Ok(s)) // Compiler complains here
//Ok(jsonResponse)
}
}
}
答案 0 :(得分:1)
使用jsonResponse.flatMap而不是map