简体   繁体   中英

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. If the first future returns a result different than 1 then the second future should never be executed. But I'm getting the following compilation error in 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

  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,因为您正在创建另一个未来。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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