foldAsync
Just like fold
but receives a function that results in a Future
to the next value.
Signature
Source.foldAsync
Flow.foldAsync
Description
Just like fold
but receives a function that results in a Future
to the next value.
Warning
Note that the zero
value must be immutable, because otherwise the same mutable instance would be shared across different threads when running the stream more than once.
Example
foldAsync
is typically used to ‘fold up’ the incoming values into an aggregate asynchronously. For example, you might want to summarize the incoming values into a histogram:
- Scala
-
source
import akka.actor.ActorSystem import akka.stream.scaladsl.Source import scala.concurrent.{ ExecutionContext, Future } case class Histogram(low: Long = 0, high: Long = 0) { def add(i: Int): Future[Histogram] = if (i < 100) Future { copy(low = low + 1) } else Future { copy(high = high + 1) } } Source(1 to 150).foldAsync(Histogram())((acc, n) => acc.add(n)).runForeach(println) // Prints: Histogram(99,51)
- Java
Reactive Streams semantics
emits when upstream completes and the last Future
is resolved
backpressures when downstream backpressures
completes when upstream completes and the last Future
is resolved