Skip to content

Instantly share code, notes, and snippets.

@mookid8000
Last active April 4, 2017 02:51
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 mookid8000/e13a9dbdc60fc57cac7f62e49dfb09d2 to your computer and use it in GitHub Desktop.
Save mookid8000/e13a9dbdc60fc57cac7f62e49dfb09d2 to your computer and use it in GitHub Desktop.
ToListAsync task sequence extension for C#
public static class TaskExtensions
{
public static async Task<List<TItem>> ToListAsync<TItem>(this IEnumerable<Task<TItem>> items)
{
var tasks = items.Select(i => i).ToArray();
await Task.WhenAll(tasks);
return tasks.Select(t => t.Result).ToList();
}
}
/// usage:
var viewManagerAndPositions = await _viewManagers
.Select(async vm => new { ViewManager = vm, Position = await vm.GetPosition() })
.ToListAsync();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment