You can use Await.result
and Future.traverse
to accomplish this.
-- create one big Future containing all the responses
val future = Future.traverse(context.children) { c => (c ? "foo")(5 seconds) }
-- now wait for it to return
val result = Await.result(future, 1 second)
Future.traverse
is key here. Slightly simplifying, it takes Traversable[A]
as it's first argument and a function A => Future[B]
as it's second, then returns one big Future[Traversable[B]]
which will complete once all the individual Future
s complete and contain all the B
results of the function.
Note that if any of the Future
s fail, the combined Future
will fail as well, which may or may not be what you want.
Also, it's usually recommended not to use Await
as it introduces a sequential bottleneck in your asynchronous Actors
, instead use pipeTo
(or onComplete
etc...) to send the result of the Future
as a message to your Actor
and then handle it accordingly.