so I have noticed there are then and map in the streamext
the obvious difference i can see is that "map" takes a FnMut returning T, resulting in a stream of T, and "then" a FnMut returning a future, resulting in a stream of T. However, there doesnt seem to be any problem to pass a FnMut returning a future to map, so I dont quite see what then does that map doesnt.
The only other difference i noticed is that i can use map with buffer(unordered) but I cant use that with then, presumably because then does not result in a stream of futures, and as such cant be buffered.
So in the end, when would i use which?
specifically, which one should i use if i want to process some http requests in parallel? "then" or (map with buffering)?
Well, but that would result in a stream whose items are futures.
The thing that
then does is give you a stream where awaiting the next item will also already execute (i.e. await) the future that the mapped closure returned for that item.
Neither introduces parallelism. Notably, when then using
then, and then consuming the stream (awaiting all its items), the execution is a strict sequence of alternatingly awaiting the next item of the original stream, and then processing it with the mapped function and executing that function’s returned future, only after that moving on to the next item.
Well, at least
map doesn’t force the sequenced execution of the futures yet that would have become stream items, so you could still introduce parallelism afterwards. For example, as you noted yourself, you can use it with
buffer, and that will result in execution of the futures in parallel (well, concurrently, at least), after all.