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))
}