3 ms·
Surprised to see a 2022 post about this without mentioning IAsyncEnumerable which is the companion to IEnumerable for async and allows async lazy eval of *each
by danjc 4y ago
Surprised to see a 2022 post about this without mentioning IAsyncEnumerable which is the companion to IEnumerable for async and allows async lazy eval of *each iteration*.
We’re using it extensively and coupled with the syntactic sugar that enables its consumption in foreach, it makes the code look as elegant as a traditional synchronous enumeration.
- DeathArrow 4y agoYou should read the article, not just the title: "And to muddy the waters just a little, not all iterators are synchronous; there’s also an IAsyncEnumerable interface (you can loop through it with await foreach)."
- bouke 4y agoEven though it is mentioned in the article, it deserves more than just mention. I find the Async LINQ extensions somewhat lacking. I need to have IAsyncEnumerable all the way down: returning an array in `SelectMany` is not possible. Sometimes you mix and match with synchronous code and the `ToAsyncEnumerable()` isn't elegant.
- WorldMaker 4y agoHave you tried importing the System.Interactive.Async package? [0] It's sometimes referred to as "Ix" (Interactive Extensions) to relate it to its older brother System.Reactive (ReactiveX or Rx). It adds a ton of LINQ-style extensions that make flowing between async and sync code easier. Despite the "System" name you always have to install it from NuGet and it's not just available out of the box, but it lights up so many LINQ extensions when installed and paired with a `using System.Linq.Async;`. Though I also think IAsyncEnumerable is even better when paired with Rx, too, as some things are easier to express as Observable "in the middle" as an AsyncEnumerable->Observable->AsyncEnumerable "sandwich". (You can sort of think of it as an pullable Event Source being spread into a push-based message bus/pipeline and then being collected back into a pullable Result feed.) [0] https://www.nuget.org/packages/System.Interactive.Async/ https://www.nuget.org/packages/System.Interactive.Async/