Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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);
- }
Add Comment
Please, Sign In to add comment