UtsavShah UtsavShah - 1 year ago 84
Scala Question

How to consume a list of futures as soon as one completes in Scala

I have a bunch of Futures that can complete at extremely different times, something like this:

val results = task.getAssignedFiles map {
file: File => Future[Result] {
<heavy computation>

Now I want to iterate over
as soon as one of the input futures completes, basically poll all the futures in results until one completes, do some processing, and continue until all of them are done. Something like:

while (!results.allCompleted) {
one = results.firstCompletedFuture

Answer Source

You're looking for firstCompletedOf.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download