pekko/akka-docs/src/main/paradox/stream/operators/Sink/headOption.md
Muskan Gupta 1ff619259f
Adding example of headOption operator (#29717)
Co-authored-by: Johan Andrén <johan@markatta.com>
2020-10-12 09:27:46 +02:00

1.3 KiB

Sink.headOption

Materializes into a @scala[Future[Option[T]]] @java[CompletionStage<Optional<T>>] which completes with the first value arriving wrapped in @scala[Some] @java[Optional], or @scala[a None] @java[an empty Optional] if the stream completes without any elements emitted.

@refSink operators

Signature

@apidocSink.headOption { scala="#headOption[T]:akka.stream.scaladsl.Sink[T,scala.concurrent.Future[Option[T]]]" java="#headOption()" }

Description

Materializes into a @scala[Future[Option[T]]] @java[CompletionStage<Optional<T>>] which completes with the first value arriving wrapped in @scala[Some] @java[Optional], or @scala[a None] @java[an empty Optional] 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.

Scala
@@snip HeadOption.scala { #headoption }
Java
@@snip SinkDocExamples.java { #headoption }

Reactive Streams semantics

@@@div { .callout }

cancels after receiving one element

backpressures never

@@@