[API Proposal]: Task.WhenAll to return results in a strongly typed tuple #100504
Labels
api-suggestion
Early API idea and discussion, it is NOT ready for implementation
area-System.Threading.Tasks
needs-further-triage
Issue has been initially triaged, but needs deeper consideration or reconsideration
Milestone
Background and motivation
A very common scenario is to run multiple asynchronous operations in parallel and continue once all of them returned their results. Currently, the only API we have for such scenarios is the
Task.WhenAll
, but it loses the type information of the individualTask
s because it operates on an array ofTask
s of the same type.If the
Task
s are of different types, await-ing them in parallel and capturing their results becomes cumbersome.With the ability to deconstruct tuples at assignment, there could be an API to run multiple
Task
s of different types in parallel and return their results as a tuple which can be deconstructed immediately without having to hold on to intermediateTask<T>
references.API Proposal
API Usage
Alternative Designs
I know that this wouldn't quite work because the
Task.WhenAll
API already hasparams
overload, so there would be ambiguity or unwanted preference between the overloads, but maybe the providedTask<T>
parameters could be wrapped in aValueTuple
to avoid that overload collision, which would look something like this:I'll admit, this does look a little weird from the caller's perspective though.
Another alternative is to use a different method name, such as
WhenEach
(though it rather rhymes withforeach
and is a new API planned for .NET 9 ( #61959 ) returningTask
s via anIAsyncEnumerable
as they complete, so returning aTuple
/ValueTuple
there might be confusing, plus, I'd assume it will also have aparams
overload and would meet with the same problem as theWhenAll
API).So,
WhenEvery
,WhenSome
,WhenAllIndividual
?The text was updated successfully, but these errors were encountered: