简体   繁体   English

Play for Scala中的嵌套期货无法编译

[英]Nested futures in Play for Scala does not compile

This Action.async method in Play for Scala should execute the second future only if the first future returns 1 , that's why they are nested. 仅当第一个Action.async返回1 ,Play for Scala中的Action.async方法才应执行第二个Action.async ,这就是它们被嵌套的原因。 If the first future returns a result different than 1 then the second future should never be executed. 如果第一个Future返回的结果与1不同,则永远不要执行第二个Future。 But I'm getting the following compilation error in f2.map . 但是我在f2.map遇到以下编译错误。 Why is this error and how to fix it? 为什么会出现此错误以及如何解决?

type mismatch; 类型不匹配; found : scala.concurrent.Future[scala.concurrent.Future[play.api.mvc.Result]] required: play.api.mvc.Result 找到:scala.concurrent.Future [scala.concurrent.Future [play.api.mvc.Result]]必需:play.api.mvc.Result

  def index = Action.async { request =>

    val f1 =  Future {1}
    f1.map {
      access => if (access==1) {
           val f2 = Future {2} 
           f2.map {   // <-- compilation error
              result => {
                 val json = JsObject(Seq(
                      "acc" -> JsNumber(access),
                      "ret" -> JsString("0")
                  ))
                  Ok(json)
               }
          }
      }
      else {
          val json = JsObject(Seq(
              "acc" -> JsNumber(0),
              "ret" -> JsString("0")
          ))
          Future.successful(Ok(json))

      }
    }
  }

您基本上就在那儿-在f1上只有flatMap而不是map,因为您正在创建另一个未来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM