The new C# 8.0 and dotnet core 3 has this new feature of AsyncStreams (IAsyncEnumerable<T>
). My understanding it that it provides a way to asynchronously process items in a stream. But would I not be able to do that with IEnumerable<Task<T>>
?
what’s the difference between these two approaches?
WhenAll(IEnumerable<Task>) Creates a task that will complete when all of the Task objects in an enumerable collection have completed. WhenAll(Task[]) Creates a task that will complete when all of the Task objects in an array have completed.
IAsyncEnumerable<T> InterfaceExposes an enumerator that provides asynchronous iteration over values of a specified type.
Async methods are intended to be non-blocking operations. An await expression in an async method doesn't block the current thread while the awaited task is running. Instead, the expression signs up the rest of the method as a continuation and returns control to the caller of the async method.
ForEach doesn't play particularly well with async (neither does LINQ-to-objects, for the same reasons). In this case, I recommend projecting each element into an asynchronous operation, and you can then (asynchronously) wait for them all to complete.
With a return type of IEnumerable<Task> you would return a blocking enumerator though the individual results are non-blocking.
With IAsyncEnumerable the enumerator itself is non-blocking, providing more flexibility for the source of the enumerable to go async.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With