我正在使用:
目前,我正在使用来自Future的scala.concurrent._类,但我愿意尝试另一个API。
我很难将多个期货的结果组合成一个列表(String,String)。
以下Controller方法成功地将单个未来的结果返回给HTML:
def test = Action { implicit request =>
queryForm.bindFromRequest.fold(
formWithErrors => Ok("Error!"),
query => {
Async {
getSearchResponse(query, 0).map { response =>
Ok(views.html.form(queryForm,
getAuthors(response.body, List[(String, String)]())))
}
}
})
}方法getSearchResult(String, Int)执行一个web调用并返回一个Futureplay.api.libs.ws.Response。方法getAuthors(String, List[(String, String)])将列表(字符串、字符串)返回给HTML。
现在,我试图在一个getSearchResult(String, Int)循环中调用for,以获得多个响应体。下面应该给出我想要做的事情,但是我得到了一个编译时错误:
def test = Action { implicit request =>
queryForm.bindFromRequest.fold(
formWithErrors => Ok("Error!"),
query => {
Async {
val authors = for (i <- 0 to 100; if i % 10 == 0) yield {
getSearchResponse(query, i)
}.map { response =>
getAuthors(response.body, List[(String, String)]())
}
Ok(views.html.form(queryForm, authors))
}
})
}类型不匹配;找到: scala.collection.immutable.IndexedSeq[scala.concurrent.Future[List(String,字符串)]]要求:列表(字符串,字符串)
如何将几个Future Result**?**对象的响应映射到单个
发布于 2013-02-24 01:07:58
通过结果类型的列表或其他集合创建未来参数。
来自here
在游戏1中,您可以这样做:
F.Promise<List<WS.HttpResponse>> promises = F.Promise.waitAll(remoteCall1, remoteCall2, remoteCall3);
// where remoteCall1..3 are promises
List<WS.HttpResponse> httpResponses = await(promises); // request gets suspended here在游戏2中,减直接:
val httpResponses = for {
result1 <- remoteCall1
result2 <- remoteCall2
} yield List(result1, result2)https://stackoverflow.com/questions/15047533
复制相似问题