首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >scala,play,期货:组合多个期货的结果

scala,play,期货:组合多个期货的结果
EN

Stack Overflow用户
提问于 2013-02-24 00:57:48
回答 1查看 9.1K关注 1票数 11

我正在使用:

  • 斯卡拉2.10
  • 播放2.1

目前,我正在使用来自Futurescala.concurrent._类,但我愿意尝试另一个API。

我很难将多个期货的结果组合成一个列表(String,String)。

以下Controller方法成功地将单个未来的结果返回给HTML:

代码语言:javascript
复制
  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,以获得多个响应体。下面应该给出我想要做的事情,但是我得到了一个编译时错误:

代码语言:javascript
复制
  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**?**对象的响应映射到单个

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-02-24 01:07:58

通过结果类型的列表或其他集合创建未来参数。

来自here

在游戏1中,您可以这样做:

代码语言:javascript
复制
    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中,减直接:

代码语言:javascript
复制
    val httpResponses = for {
  result1 <- remoteCall1
  result2 <-  remoteCall2
} yield List(result1, result2)
票数 11
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15047533

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档