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.
- Scala
-
source
val source = Source.empty val result: Future[Option[Int]] = source.runWith(Sink.headOption) result.foreach(println) //None
- Java
Reactive Streams semantics
cancels after receiving one element
backpressures never