在我的後端控制器功能,我有以下代碼:如何解決Scala的異步行爲
def getContentComponents = Action.async {
contentComponentDTO.list().map { contentComponentsFuture =>
contentComponentsFuture.foreach(contentComponentFuture =>
contentComponentFuture.typeOf match {
case 5 => contentComponentDTO.getContentComponentText(contentComponentFuture.id.get).map(text => {
contentComponentFuture.text = text.text
println(contentComponentFuture.text)
})
}
)
Ok(Json.toJson(contentComponentsFuture))
}
}
的問題是,上面的東西完成之前OK()
被調用。有沒有一種智能的方法可以等到foreach
完成?
謝謝
這是兩個不同的問題與不同的問題!因此,這是兩個問題看起來類似的原因
這遠不是這個函數的第一個問題上。我強烈建議先閱讀Scala和Play教程,然後再問 – cchantep
[scala Returns Future \ [Unit \]而不是Future \ [ContentComponentModel \]]的可能重複(https://stackoverflow.com/questions/45835922/scala- return-futureunit-instead-of-futurecontentcomponentmodel) – cchantep