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

@ -43,6 +43,7 @@ import java.time.Duration;
import java.util.Arrays;
import java.util.Comparator;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
class SourceOrFlow {
private static ActorSystem system = null;
@ -187,19 +188,23 @@ class SourceOrFlow {
// #scan
}
// #scan-async
CompletionStage<Integer> asyncFunction(int acc, int next) {
return CompletableFuture.supplyAsync(() -> acc + next);
}
// #scan-async
void scanAsyncExample() {
// #scanAsync
// #scan-async
Source<Integer, NotUsed> source = Source.range(1, 5);
source
.scanAsync(0, (acc, x) -> CompletableFuture.completedFuture(acc + x))
.runForeach(System.out::println, materializer);
source.scanAsync(0, (acc, x) -> asyncFunction(acc, x)).runForeach(System.out::println, system);
// 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
}
static // #conflateWithSeed-type