Wait for any of the futures in Scala?

Suppose I need to perform several parallel tasks in Scala. Each task makes a blocking call (for example, Process.waitFor ). Now I would like to wait until one of the tasks is completed.

As I understand it, I have to use Scala Future to transfer the task. Is there any API in Scala to wait for any of the given Futures ?

+6
source share
1 answer

There is a built-in method for this:

 Future.firstCompletedOf(yourFutures) 

from the document :

Returns the future to the result of the first future on the list that is completed.

Please note that this will not interrupt all other futures, so you must cancel them yourself if you need to.

+13
source

All Articles