Skip to content

Commit 4d3eabb

Browse files
committed
feature: Useful helper for async tasks
1 parent fa69b3d commit 4d3eabb

File tree

1 file changed

+136
-0
lines changed
  • OpenActive.Server.NET/OpenBookingHelper/Async

1 file changed

+136
-0
lines changed
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
using System;
2+
using System.Runtime.CompilerServices;
3+
using System.Threading.Tasks;
4+
5+
namespace OpenActive.Server.NET.OpenBookingHelper
6+
{
7+
// https://www.meziantou.net/get-the-result-of-multiple-tasks-in-a-valuetuple-and-whenall.htm
8+
public static partial class TaskEx
9+
{
10+
public static async Task<(T0, T1)> WhenAll<T0, T1>(Task<T0> task0, Task<T1> task1)
11+
{
12+
if (task0 is null)
13+
throw new ArgumentNullException(nameof(task0));
14+
if (task1 is null)
15+
throw new ArgumentNullException(nameof(task1));
16+
17+
await Task.WhenAll(task0, task1).ConfigureAwait(false);
18+
return (task0.Result, task1.Result);
19+
}
20+
21+
public static async Task<(T0, T1, T2)> WhenAll<T0, T1, T2>(Task<T0> task0, Task<T1> task1, Task<T2> task2)
22+
{
23+
if (task0 is null)
24+
throw new ArgumentNullException(nameof(task0));
25+
if (task1 is null)
26+
throw new ArgumentNullException(nameof(task1));
27+
if (task2 is null)
28+
throw new ArgumentNullException(nameof(task2));
29+
30+
await Task.WhenAll(task0, task1, task2).ConfigureAwait(false);
31+
return (task0.Result, task1.Result, task2.Result);
32+
}
33+
34+
public static async Task<(T0, T1, T2, T3)> WhenAll<T0, T1, T2, T3>(Task<T0> task0, Task<T1> task1, Task<T2> task2, Task<T3> task3)
35+
{
36+
if (task0 is null)
37+
throw new ArgumentNullException(nameof(task0));
38+
if (task1 is null)
39+
throw new ArgumentNullException(nameof(task1));
40+
if (task2 is null)
41+
throw new ArgumentNullException(nameof(task2));
42+
if (task3 is null)
43+
throw new ArgumentNullException(nameof(task3));
44+
45+
await Task.WhenAll(task0, task1, task2, task3).ConfigureAwait(false);
46+
return (task0.Result, task1.Result, task2.Result, task3.Result);
47+
}
48+
49+
public static async Task<(T0, T1, T2, T3, T4)> WhenAll<T0, T1, T2, T3, T4>(Task<T0> task0, Task<T1> task1, Task<T2> task2, Task<T3> task3, Task<T4> task4)
50+
{
51+
if (task0 is null)
52+
throw new ArgumentNullException(nameof(task0));
53+
if (task1 is null)
54+
throw new ArgumentNullException(nameof(task1));
55+
if (task2 is null)
56+
throw new ArgumentNullException(nameof(task2));
57+
if (task3 is null)
58+
throw new ArgumentNullException(nameof(task3));
59+
if (task4 is null)
60+
throw new ArgumentNullException(nameof(task4));
61+
62+
await Task.WhenAll(task0, task1, task2, task3, task4).ConfigureAwait(false);
63+
return (task0.Result, task1.Result, task2.Result, task3.Result, task4.Result);
64+
}
65+
66+
public static async Task<(T0, T1, T2, T3, T4, T5)> WhenAll<T0, T1, T2, T3, T4, T5>(Task<T0> task0, Task<T1> task1, Task<T2> task2, Task<T3> task3, Task<T4> task4, Task<T5> task5)
67+
{
68+
if (task0 is null)
69+
throw new ArgumentNullException(nameof(task0));
70+
if (task1 is null)
71+
throw new ArgumentNullException(nameof(task1));
72+
if (task2 is null)
73+
throw new ArgumentNullException(nameof(task2));
74+
if (task3 is null)
75+
throw new ArgumentNullException(nameof(task3));
76+
if (task4 is null)
77+
throw new ArgumentNullException(nameof(task4));
78+
if (task5 is null)
79+
throw new ArgumentNullException(nameof(task5));
80+
81+
await Task.WhenAll(task0, task1, task2, task3, task4, task5).ConfigureAwait(false);
82+
return (task0.Result, task1.Result, task2.Result, task3.Result, task4.Result, task5.Result);
83+
}
84+
85+
public static async Task<(T0, T1, T2, T3, T4, T5, T6)> WhenAll<T0, T1, T2, T3, T4, T5, T6>(Task<T0> task0, Task<T1> task1, Task<T2> task2, Task<T3> task3, Task<T4> task4, Task<T5> task5, Task<T6> task6)
86+
{
87+
if (task0 is null)
88+
throw new ArgumentNullException(nameof(task0));
89+
if (task1 is null)
90+
throw new ArgumentNullException(nameof(task1));
91+
if (task2 is null)
92+
throw new ArgumentNullException(nameof(task2));
93+
if (task3 is null)
94+
throw new ArgumentNullException(nameof(task3));
95+
if (task4 is null)
96+
throw new ArgumentNullException(nameof(task4));
97+
if (task5 is null)
98+
throw new ArgumentNullException(nameof(task5));
99+
if (task6 is null)
100+
throw new ArgumentNullException(nameof(task6));
101+
102+
await Task.WhenAll(task0, task1, task2, task3, task4, task5, task6).ConfigureAwait(false);
103+
return (task0.Result, task1.Result, task2.Result, task3.Result, task4.Result, task5.Result, task6.Result);
104+
}
105+
106+
public static TaskAwaiter<(T1, T2)> GetAwaiter<T1, T2>(this ValueTuple<Task<T1>, Task<T2>> tasks)
107+
{
108+
return WhenAll(tasks.Item1, tasks.Item2).GetAwaiter();
109+
}
110+
111+
public static TaskAwaiter<(T1, T2, T3)> GetAwaiter<T1, T2, T3>(this ValueTuple<Task<T1>, Task<T2>, Task<T3>> tasks)
112+
{
113+
return WhenAll(tasks.Item1, tasks.Item2, tasks.Item3).GetAwaiter();
114+
}
115+
116+
public static TaskAwaiter<(T1, T2, T3, T4)> GetAwaiter<T1, T2, T3, T4>(this ValueTuple<Task<T1>, Task<T2>, Task<T3>, Task<T4>> tasks)
117+
{
118+
return WhenAll(tasks.Item1, tasks.Item2, tasks.Item3, tasks.Item4).GetAwaiter();
119+
}
120+
121+
public static TaskAwaiter<(T1, T2, T3, T4, T5)> GetAwaiter<T1, T2, T3, T4, T5>(this ValueTuple<Task<T1>, Task<T2>, Task<T3>, Task<T4>, Task<T5>> tasks)
122+
{
123+
return WhenAll(tasks.Item1, tasks.Item2, tasks.Item3, tasks.Item4, tasks.Item5).GetAwaiter();
124+
}
125+
126+
public static TaskAwaiter<(T1, T2, T3, T4, T5, T6)> GetAwaiter<T1, T2, T3, T4, T5, T6>(this ValueTuple<Task<T1>, Task<T2>, Task<T3>, Task<T4>, Task<T5>, Task<T6>> tasks)
127+
{
128+
return WhenAll(tasks.Item1, tasks.Item2, tasks.Item3, tasks.Item4, tasks.Item5, tasks.Item6).GetAwaiter();
129+
}
130+
131+
public static TaskAwaiter<(T1, T2, T3, T4, T5, T6, T7)> GetAwaiter<T1, T2, T3, T4, T5, T6, T7>(this ValueTuple<Task<T1>, Task<T2>, Task<T3>, Task<T4>, Task<T5>, Task<T6>, Task<T7>> tasks)
132+
{
133+
return WhenAll(tasks.Item1, tasks.Item2, tasks.Item3, tasks.Item4, tasks.Item5, tasks.Item6, tasks.Item7).GetAwaiter();
134+
}
135+
}
136+
}

0 commit comments

Comments
 (0)