Given a method that returns a Future
like this...
def remove(id: String): Future[Option[User]] = Future {
// removes and returns the user identified by `id`
}
... how do I invoke it again and again until it returns a Future
value containing None
?
EDIT
Perhaps it is worth to mention that I don't need to collect the results. I just need to invoke the method as long as it finds an user to remove. The idea would be to have a loop
that stops when remove
returns Future[None]
.
Someone commented earlier that there's no point.
The surprise for me was that there's no quickie for lazily consuming the futures.
Future.find
is likefirstCompletedOf
, and doesn't meanfind first in traversable order
.Illustrating the utility of not blocking:
Showing the
Stream
version, which won't calculate the prefix until the consuming thread steps through the blocking Awaits:The other behavior, probably more desirable, where you get a Future that holds the result of calculating the prefix:
Use the future:
Here it is:
I hope it helps.
Stream#continually
to do the same thing endlessly, andStream#takeWhile
to halt it at a certain point. http://www.scala-lang.org/api/2.11.0/index.html#scala.collection.immutable.Stream