0
裏面我已經異步播放Action
,從datbase檢索數據,用油滑。和光滑,很明顯,使用Future
,以免發生堵塞:製作獨立的油滑的查詢單玩動作
def show(id: Long) = Action.async {
db.run(entities.filter(_.id === id).result.headOption).map {
case None => templateFor("NEW_OBJECT")
case Some(x) => Ok(x)
}
def templateFor(code: String): Future[Result] = {
db.run(templates.filter(_.code === code).result.headOption).map {
case None => InternalServerError("No template")
case Some(x) => Ok(x)
}
}
的問題是,調用templateFor()
回報Future
,所以整個Action
回報Future[Future[Result]]
它通過發揮不期望的。所以,我想擺脫嵌套Future
。簡單的方法是完成Await
,但我想避免不必要的阻塞。這將是很好,如果我將能夠採取由templateFor()
函數產生Future[Result]
並返回它從我的Action
完好,從而取代外Future
它。
'flatMap'吧:) – ipoteka
作爲@ipoteka說:https://cdn.meme.am/cache/instances/folder331/500x/67321331.jpg :) –