I have some side-effectful function,
def f(): Future[Int] = {
val n = Random.nextInt()
println(s\"Generated $n\")
Future(n)
}
and I
Instead of using Stream I suggest using another approach. Using The Future's filter and recoverWith recursively:
def findFirst[A](futureGen: => Future[A], predicate: A => Boolean): Future[A] = {
futureGen.filter(predicate).recoverWith { case _ => findFirst(futureGen, predicate) }
}
findFirst(f, success)
This will call the Futures one after the other until 'success' will return true.