Created
October 17, 2012 23:58
-
-
Save eboto/3909092 to your computer and use it in GitHub Desktop.
Using Futures
This file contains hidden or 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
// This is not a particularly impressive example of using futures. | |
def getMetrics: List[EgraphsMetric[Int]] = { | |
import akka.pattern.{ ask, pipe } | |
val futureMetrics: List[Future[EgraphsMetric[Int]]] = for (actor <- actors: List[ActorRef]) yield { | |
implicit val timeout = Timeout(5 seconds) | |
for { | |
metric <- ask(actor, GetMetric).mapTo[EgraphsMetric[Int]] | |
} yield metric | |
} | |
for (futureMetric <- futureMetrics) yield Await.result(futureMetric, 10.seconds) | |
} | |
List[Future[EgraphsMetric[Int]]] | |
val futureMetrics: Future[List[EgraphsMetric[Int]]] | |
val futureValues: Future[List[JsValue]] = for ( | |
metrics:List[EgraphMetric] <- futureMetrics; | |
metric:EgraphMetric[Int] <- metrics | |
) yield { | |
metric.toJson | |
} | |
val futureJsArray: Future[Json] = for(jsValuesList <- futureValues) yield Json.toJson(jsValuesList.toSeq) | |
val jsArray:JsArray = Await.result(futureJsArray, 5.seconds) | |
Ok(jsArray) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment