Created
January 30, 2013 13:07
-
-
Save bthuillier/4673180 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def test = Action { | |
// Solution la plus générique | |
Async { | |
val futureA = WS.url("xx").get | |
val futureB = WS.url("yy").get | |
val resultAB = for { | |
resA <- futureA | |
resB <- futureB | |
} yield (resA, resB) | |
resultAB.map(_ match { | |
case (resA, resB) => { | |
Ok(Json.obj("a" -> resA.json, "b" -> resB.json)) | |
} | |
}) | |
} | |
// Bien quand il n'y a qu'un ou deux niveaux | |
Async { | |
WS.url("xx").get.flatMap(resA => | |
WS.url("yy").get().map(resB => | |
Ok(Json.obj("a" -> resA.json, "b" -> resB.json)) | |
) | |
) | |
} | |
// Autre méthode | |
Async { | |
val futureA = WS.url("xx").get | |
val futureB = WS.url("yy").get | |
val futures = List(futureA, futureB) | |
val results = Future.sequence(futures) // List[Future[X]] => Future[List[X]] | |
// Fait quelque chose avec results | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment