Document scanAsync (#28239)

* Document scanAsync

* Include async function in scanAsync example
This commit is contained in:
Christopher Batey 2019-11-26 11:25:20 +00:00 committed by Arnout Engelen
parent 82db446bb7
commit 09a97ce1c9
6 changed files with 52 additions and 39 deletions

View file

@ -3,26 +3,34 @@
*/
package docs.stream.operators.sourceorflow
import akka.stream.scaladsl.Source
import scala.concurrent.ExecutionContext
import scala.concurrent.Future
object ScanAsync {
def scanAsyncExample(): Unit = {
import akka.actor.ActorSystem
implicit val system: ActorSystem = ActorSystem()
implicit val ec: ExecutionContext = system.dispatcher
//#scan-async
def asyncFunction(acc: Int, next: Int): Future[Int] = Future {
acc + next
}
//#scanAsync
val source = Source(1 to 5)
source.scanAsync(0)((acc, x) => Future.successful(acc + x)).runForeach(println)
source.scanAsync(0)((acc, x) => asyncFunction(acc, x)).runForeach(println)
// 0 (= 0)
// 1 (= 0 + 1)
// 3 (= 0 + 1 + 2)
// 6 (= 0 + 1 + 2 + 3)
// 10 (= 0 + 1 + 2 + 3 + 4)
// 15 (= 0 + 1 + 2 + 3 + 4 + 5)
//#scanAsync
//#scan-async
}
}