1
votes

I have the following method that returns a list of Strings in a Play for Scala application:

def example = Action.async { request =>

  val access = getAccess()

  if (access > 0) {
          val future = MyObject.intensiveMethod
          future.map {
              result => {
                  val list = result.asInstanceOf[List[String]]
                  val json = JsObject(Seq(
                      "list" -> Json.toJson(list)
                  ))
                  Ok(json)
              }
          }
  }
  else {
      val json = JsObject(Seq(
          "msg" -> JsString("error!")
      ))
      Ok(json)
  }

}

The code does not compile with the following error, because if access = 0 the result is not a Future:

type mismatch; found : play.api.mvc.Result required: scala.concurrent.Future[play.api.mvc.Result]

How to fix this?

1

1 Answers

5
votes

Then wrap it with Future, e.g.:

  Future.successful(Ok(JsObject(Seq(
      "msg" -> JsString("error!")
  ))))