Guest User

Untitled

a guest
Oct 21st, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.63 KB | None | 0 0
  1. using System.Runtime.CompilerServices;
  2. using System.Threading.Tasks;
  3.  
  4. // Example 4: await IEnumerable<Task<T>>
  5.  
  6. // Just a dummy async function
  7. public static Task<string> GetSomethingAsync(int number) => Task.FromResult($"Something {number}");
  8.  
  9. // This extension method allows us to await many tasks
  10. public static TaskAwaiter<T[]> GetAwaiter<T>(this IEnumerable<Task<T>> manyTasks)
  11. => Task.WhenAll(manyTasks).GetAwaiter();
  12.  
  13. // this line will now compile and run
  14. var manyThings = await Enumerable.Range(0, 10).Select(GetSomethingAsync);
  15.  
  16. // Just to prove it, output all the things
  17. foreach(var thing in manyThings)
  18. {
  19. Console.WriteLine(thing);
  20. }
Add Comment
Please, Sign In to add comment