Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@mariusGundersen
Created October 21, 2018 11:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mariusGundersen/84f12a29e606fa14d357b3b6660fa4bf to your computer and use it in GitHub Desktop.
Save mariusGundersen/84f12a29e606fa14d357b3b6660fa4bf to your computer and use it in GitHub Desktop.
Duct-typed extension methods Example 4
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
// Example 4: await IEnumerable<Task<T>>
// Just a dummy async function
public static Task<string> GetSomethingAsync(int number) => Task.FromResult($"Something {number}");
// This extension method allows us to await many tasks
public static TaskAwaiter<T[]> GetAwaiter<T>(this IEnumerable<Task<T>> manyTasks)
=> Task.WhenAll(manyTasks).GetAwaiter();
// this line will now compile and run
var manyThings = await Enumerable.Range(0, 10).Select(GetSomethingAsync);
// Just to prove it, output all the things
foreach(var thing in manyThings)
{
Console.WriteLine(thing);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment