Created
October 21, 2018 11:18
-
-
Save mariusGundersen/84f12a29e606fa14d357b3b6660fa4bf to your computer and use it in GitHub Desktop.
Duct-typed extension methods Example 4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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