Sink.headOption
Materializes into a Future[Option[T]]
which completes with the first value arriving wrapped in Some
, or a None
if the stream completes without any elements emitted.
Signature¶
Description¶
Materializes into a Future[Option[T]]
which completes with the first value arriving wrapped in Some
, or a None
if the stream completes without any elements emitted.
Example¶
In this example there is an empty source i.e. it does not emit any element and to handle it we have used headOption operator which will complete with None.
sourceval source = Source.empty
val result: Future[Option[Int]] = source.runWith(Sink.headOption)
result.foreach(println)
// None
sourceSource<Integer, NotUsed> source = Source.empty();
CompletionStage<Optional<Integer>> result = source.runWith(Sink.headOption(), system);
result.thenAccept(System.out::println);
// Optional.empty
Reactive Streams semantics¶
cancels after receiving one element
backpressures never
1.1.3