I sometimes find myself in a situation where I have some Stream[X]
, and a function X => Future Y
, that I\'d like to combine to a Future[Stream
The accepted answer is no longer valid as the modern version of Scalaz traverse()
behaves differently and tries to consume the entire stream on the invocation time.
As to the question I would say that it's impossible to achieve this a truly non-blocking fashion.
Future[Stream[Y]]
cannot be resolved until Stream[Y]
is available. And since Y
is produced asynchronously by the function X => Future[Y]
you cannot get Y
without blocking on the time when you traverse Stream[Y]
. That means that either all the Future[Y]
must be resolved before resolving Future[Stream[Y]]
(which requires consuming the entire stream), or you must allow blocks to occur while traversing Stream[Y]
(on items whose underlying futures aren't completed yet).
But if we allow for blocking on the traversing then what would be the definition of the completion of the resulting future? From that perspective it could be the same as Future.successful(BlockingStream[Y])
. That's in turn semantically equal to the original Stream[Future[Y]]
.
In other words, I think there is an issue in the question itself.