Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In scala, how do you transform a list of futures into a future that returns the first successful future? [duplicate]

Tags:

scala

future

All futures may be successful eventually (and some may fail), but we want the first successful one. And want to represent that result as a future. This future will fail if all the futures in the list fail.

like image 640
yalis Avatar asked Dec 31 '22 08:12

yalis


1 Answers

As indicated the documentation, Future.firstCompletedOf is provided.

import scala.concurrent.{ExecutionnContext, Future }

def foo[T](f: => Seq[Future[T]])(implicit ec: ExecutionContext): Future[T] =
  Future.firstCompletedOf(f)
like image 137
cchantep Avatar answered Apr 19 '23 22:04

cchantep