With "async everywhere", the ability to fire off multiple heterogeneous operations is becoming more frequent. The current Task.WhenAll
method returns its results as an array and requires all tasks to return the same kind of object which makes its usage a bit clumsy. I'd like to be able to write...
var (i, s, ...) = await AsyncExtensions.WhenAll(
GetAnIntFromARemoteServiceAsync(),
GetAStringFromARemoteServiceAsync(),
... arbitrary list of tasks
);
Console.WriteLine($"Generated int {i} and string {s} ... and other things");
The best implementation I've been able to come up with is
public static class AsyncExtensions
{
public static async Task<(TA , TB )> WhenAll<TA, TB>(Task<TA> operation1, Task<TB> operation2)
{
return (await operation1, await operation2;
}
}
This has the disadvantage that I need to implement separate methods of up to N parameters. According to this answer that's just a limitation of using generics. This implementation also has the limitation that void-returning Tasks can't be supported but that is less of a concern.
My question is: Do any of the forthcoming language features allow a cleaner approach to this?