Play 2.2 - Scala - How to associate futures with a controller action

I have 3 futures of type Response. The first future returns a JsValue, which determines whether future 2 and future 3 will be executed or only future 3 will be executed.

pseudocode: If future 1, then {future2 and future 3} else future 3

I try to do this in the game framework action, which means to use the result of futures later, I cannot use onSuccess, onFailure and onComplete, because they all return Unit, not the actual JsValue from the last future.

I tried to do this using map () and andThen, but I am Scala noob, and I think I could not do this because I always just missed a bit. Here is my current approach that doesn't work!

def addSuggestion(indexName: String, suggestion: String) = Action.async {
  val indexExistsQuery: IndexExistsQuery = new IndexExistsQuery(indexName);
  val addSuggestionQuery: AddSuggestionQuery = new AddSuggestionQuery(indexName, suggestion)
  val indexCreationQuery: CreateCompletionsFieldQuery = new CreateCompletionsFieldQuery(indexName)

  val futureResponse: Future[Response] = for {
    responseOne <- EsClient.execute(indexExistsQuery)
    responseTwo <- EsClient.execute(indexCreationQuery) if (indexExistsQuery.getBooleanResult(indexExistsQuery.getResult(responseOne)))
    responseThree <- EsClient.execute(addSuggestionQuery)
  } yield responseThree

  futureResponse.map { response =>
  Ok("Feed title: " + (response.json))
}
+4
2

:

checkIndexExists() map {
  case true => Future.successful()
  case false => createIndex()
} flatMap { _ =>
  query()
}.map { response =>
  Ok("Feed title: " + (response.json))
}.recover { 
  case _ => Ok("bla") 
} 

, . , Future[Boolean], . , Boolean. , , . , . , Future (Future[Future[Response]]). flatMap, , Future[Response]. .

( MeiSign):

  EsClient.execute(indexExistsQuery) map { response =>
      if (indexExistsQuery.getBooleanResult(indexExistsQuery.getResult(response))) Future.successful(Response)
      else EsClient.execute(indexCreationQuery)
    } flatMap { _ =>
      EsClient.execute(addSuggestionQuery)
    } map { response: Response =>
      Ok("Feed title: " + (response.json))
    }
+3

, , , Iam Await.result(), . - , , , .

def addSuggestion(indexName: String, suggestion: String) = Action.async {
  val indexExistsQuery: IndexExistsQuery = new IndexExistsQuery(indexName);
  val addSuggestionQuery: AddSuggestionQuery = new AddSuggestionQuery(indexName, suggestion)
  val indexCreationQuery: CreateCompletionsFieldQuery = new CreateCompletionsFieldQuery(indexName)

  val indexExists: Boolean = indexExistsQuery.getBooleanResult(indexExistsQuery.getResult(Await.result(EsClient.execute(indexExistsQuery), 5.second)))
  if (indexExists) {
    EsClient.execute(addSuggestionQuery).map { response => Ok("Feed title: " + (response.json)) }
  } else {
    val futureResponse: Future[Response] = for {
      responseTwo <- EsClient.execute(indexCreationQuery)
      responseThree <- EsClient.execute(addSuggestionQuery)
    } yield responseThree

    futureResponse.map { response =>
      {
        Ok("Feed title: " + (response.json))
      }
    }.recover { case _ => Ok("bla") }
  }
}
0

All Articles