-
Notifications
You must be signed in to change notification settings - Fork 3
Description
Wanted to write this down so I don't forget, though it won't be in the first version of this proposal.
We should have a helper for merging or racing multiple AsyncIterators. When next
is first called you pull from all of them, and then resolve with the first promise to settle. When next
is called again, if any of the previously-pulled promises have already settled you immediately settle with that; otherwise you pull from all the underlying iterators which you aren't currently waiting on and resolve with the first to settle.
RxJS has approximately this (apparently reasonably popular), as do some Rust libraries.
This essay points out that this is the type-theory "sum" to zip
's "product", for async iterators. That is, it's the natural extension of Promise.race
, where zip
is the natural extension of Promise.all
.
The async-std library in Rust takes the interesting approach of randomizing the order it polls the underlying streams. I'm guessing that's mostly for the case where the first stream makes all of its results available immediately, which would prevent ever getting values from the second stream even if its results were also available immediately. I don't think that's relevant here since we can (and must) hold on to results from previous calls which haven't yet been merged in, and so can ensure that in in the case of two async iterators which vended immediately-settling promises we'd alternate between them.