How to implement an efficient WhenEach that streams an IAsyncEnumerable of task results?

By using code from this article, you can implement the following: public static Task<Task<T>>[] Interleaved<T>(IEnumerable<Task<T>> tasks) { var inputTasks = tasks.ToList(); var buckets = new TaskCompletionSource<Task<T>>[inputTasks.Count]; var results = new Task<Task<T>>[buckets.Length]; for (int i = 0; i < buckets.Length; i++) { buckets[i] = new TaskCompletionSource<Task<T>>(); results[i] = buckets[i].Task; } int nextTaskIndex = -1; Action<Task<T>> continuation … Read more

Is it possible to “await yield return DoSomethingAsync()”

What you are describing can be accomplished with the Task.WhenAll method. Notice how the code turns into a simple one-liner. What happens is that each individual url begins downloading and then WhenAll is used combine those operations into a single Task which can be awaited. Task<IEnumerable<string>> DownLoadAllUrls(string[] urls) { return Task.WhenAll(from url in urls select … Read more

Factory for IAsyncEnumerable or IAsyncEnumerator

Here is another implementation of the AsyncEnumerableSource class, that doesn’t depend on the Rx library. This one depends instead on the Channel<T>, class, which is natively available in the .NET standard libraries. It has identical behavior to the Rx-based implementation. The class AsyncEnumerableSource can propagate notifications to multiple subscribers. Each subscriber can enumerate these notifications … Read more