How to get materialized value from CompletionStage without blocking

I have code:

final ActorSystem system = ActorSystem.create(“test-stream”);
final Materializer mat = ActorMaterializer.create(system);
final Source<Integer, NotUsed> source = Source.range(1, 100);
final Sink<Integer, CompletionStage> sinkTotal = Sink.fold(0, (aggr, next) -> { return aggr + next;});
final RunnableGraph<CompletionStage> graph = source.toMat(sinkTotal, Keep.right());
final CompletionStage sum =;
int total = sum.toCompletableFuture().get());

To get the final sum value, I need to do:


But get() method is blocking.
How can I access the final value without blocking the main thread?

Well if that’s all your code…? I don’t see where mat comes from? Do you just have a mistake in that it should be;?

I didn’t show ActorSystem, Materializer related code. Now its there.

It depends. If you have some javascript background you will totally fine with the callback way. But in other cases you need to wait it. The main question is; you need the result in the main thread? (Most of the cases you don’t need it there.)