1

TaskTupleAwaiter https://github.com/buvinghausen/TaskTupleAwaiter allows us to do

// For example
// Task<int> t1() { return Task.FromResult(1); }
// Task<string> t2() { return Task.FromResult("a"); }
var (r1, r2) = await (t1(), t2());

How does it compare to the following code? (Besides more concise code)

var t1task = t1();
var t2task = t2();
// var r1 = await t1task; // Edited one is actually I wanted to ask
// var r2 = await t2task;
await Task.WhenAll(t1task, t2task);  
var r1 = t1task.Result;
var r2 = t2task.Result;

The project hasn't had any update for a while. I'm wondering if I need to remove it from our repo.

ca9163d9
  • 27,283
  • 64
  • 210
  • 413

1 Answers1

2

Based on the source code await (t1(), t2()) should be the same as:

var t1task = t1();
var t2task = t2();
await Task.WhenAll(t1task, t2task);  
var r1 = t1task.Result;
var r2 = t2task.Result;

Which should be preferable compared to the original code (now commented out) in the question.

The project hasn't had any update for a while.

The source code was updated only 5 month ago, project targets latest released (at the moment of writing) version of .NET - .NET 6. I would say that for smaller projects which does not have a lot of features such maintainability schedule should be just fine, so personally I see no major reason to switch.

Guru Stron
  • 102,774
  • 10
  • 95
  • 132
  • `Task.WhenAll` needs all awaitables return the same type? In the question, `t1` and `t2` can return different types. – ca9163d9 Aug 01 '22 at 21:01
  • @ca9163d9 no, the code above will compile just just fine if `t1` and `t2` have different return types. `Task.WhenAll()` has overload which accepts just `Task` which is base class for `Task`. – Guru Stron Aug 01 '22 at 21:03
  • hmm, yes, it works. Interesting, in their page https://www.nuget.org/packages/TaskTupleAwaiter, it mentioned that `Enable using the new Value Tuple structure to write elegant code that allows async methods to be fired in parallel despite having different return types`. Just wonder at what case it's not allowed to be fired in parallel when having different return types. – ca9163d9 Aug 01 '22 at 21:07
  • @ca9163d9 _"Just wonder at what case it's not allowed to be fired in parallel when having different return types."_ - not following you here. – Guru Stron Aug 01 '22 at 21:08
  • The nuget page of TaskTupleAwaiter says that it "allows async methods to be fired in parallel despite having different return types". I'm wondering why they said this. It seems async methods can always be fired in parallel despite having different return types? – ca9163d9 Aug 01 '22 at 21:12
  • Yes ofc, return types hardly play any role in how the method executes. – Tanveer Badar Aug 03 '22 at 03:28
  • @ca9163d9 the nuget states that this _elegant_ structure allows firing in parallel despite having different return types. I would argue that the emphasis is on "elegant", not that it is impossible to fire tasks in parallel despite them having different return types. – Guru Stron Aug 03 '22 at 05:35