Let's say I have some iterator:
val nextElemIter: Iterator[Future[Int]] = Iterator.continually(...)
And I want to build a source from that iterator:
val source: Source[Future[Int], NotUsed] =
Source.fromIterator(() => nextElemIter)
So now my source emits Future
s. I have never seen futures being passed between stages in Akka docs or anywhere else, so instead, I could always do something like this:
val source: Source[Int, NotUsed] =
Source.fromIterator(() => nextElemIter).mapAsync(1)(identity /* was n => n */)
And now I have a regular source that emits T
instead of Future[T]
. But this feels hacky and wrong.
What's the proper way to deal with such situations?
mapAsync
is perfectly fine here. After all, it is intended exactly for this purpose - flattening futures into streams. – Vladimir MatveevmapAsync(1)(identity)
is proper way of doing it. – expertmapAsync
was more for futures that get created inside a stage, not coming already as a future from the outside. I might be wrong though. It just seems odd that there's noSource
builder which flattens futures by itself – Ori Popowski