Skip to content

Instantly share code, notes, and snippets.

@brikis98
Last active December 11, 2015 11:18
Show Gist options
  • Select an option

  • Save brikis98/4592742 to your computer and use it in GitHub Desktop.

Select an option

Save brikis98/4592742 to your computer and use it in GitHub Desktop.
Play Framework at LinkedIn
package controllers
import play.api.mvc.{Controller, Action, Result}
import play.api.libs.ws.WS
import play.api.libs.json.Json
import play.api.libs.concurrent.Execution.Implicits._
import scala.concurrent.Future
object Race extends Controller {
def index() = Action {
Async {
val start = System.currentTimeMillis()
def getLatency(r: Any): Long = System.currentTimeMillis() - start
val googleTime = WS.url("http://www.google.com").get().map(getLatency)
val yahooTime = WS.url("http://www.yahoo.com").get().map(getLatency)
val bingTime = WS.url("http://www.bing.com").get().map(getLatency)
Future.sequence(Seq(googleTime, yahooTime, bingTime)).map { case times =>
Ok(Json.toJson(Map("google" -> times(0), "yahoo" -> times(1), "bing" -> times(2))))
}
}
}
}
@monzonj

monzonj commented Feb 27, 2013

Copy link
Copy Markdown

Also, kind of more readable

for(
google <- googleTime;
yahoo <-yahooTime;
bing <-bingTime
) yield Ok(Json.toJson(Map("google" -> google, "yahoo" -> yahoo, "bing" -> bing)))

love,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment