Skip to content

Commit dff8b3d

Browse files
committed
Clean up
1 parent a06b3f5 commit dff8b3d

File tree

1 file changed

+20
-24
lines changed

1 file changed

+20
-24
lines changed

libs/little-forker/src/LittleForker/TaskExtensions.cs

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,37 +17,33 @@ internal static Task<T> WithCancellation<T>(this Task<T> task, CancellationToken
1717

1818
internal static async Task TimeoutAfter(this Task task, TimeSpan timeout)
1919
{
20-
using (var timeoutCancellationTokenSource = new CancellationTokenSource())
21-
{
22-
var completedTask = await Task
23-
.WhenAny(task, Task.Delay(timeout, timeoutCancellationTokenSource.Token))
24-
.ConfigureAwait(false);
25-
26-
if (completedTask == task)
27-
{
28-
timeoutCancellationTokenSource.Cancel();
29-
await task.ConfigureAwait(false);
30-
return;
31-
}
20+
using var timeoutCancellationTokenSource = new CancellationTokenSource();
21+
var completedTask = await Task
22+
.WhenAny(task, Task.Delay(timeout, timeoutCancellationTokenSource.Token))
23+
.ConfigureAwait(false);
3224

33-
throw new TimeoutException("The operation has timed out.");
25+
if (completedTask == task)
26+
{
27+
timeoutCancellationTokenSource.Cancel();
28+
await task.ConfigureAwait(false);
29+
return;
3430
}
31+
32+
throw new TimeoutException("The operation has timed out.");
3533
}
3634

3735
internal static async Task<T> TimeoutAfter<T>(this Task<T> task, TimeSpan timeout)
3836
{
39-
using (var timeoutCancellationTokenSource = new CancellationTokenSource())
37+
using var timeoutCancellationTokenSource = new CancellationTokenSource();
38+
var completedTask = await Task
39+
.WhenAny(task, Task.Delay(timeout, timeoutCancellationTokenSource.Token))
40+
.ConfigureAwait(false);
41+
if (completedTask == task)
4042
{
41-
var completedTask = await Task
42-
.WhenAny(task, Task.Delay(timeout, timeoutCancellationTokenSource.Token))
43-
.ConfigureAwait(false);
44-
if (completedTask == task)
45-
{
46-
timeoutCancellationTokenSource.Cancel();
47-
return await task.ConfigureAwait(false);
48-
}
49-
50-
throw new TimeoutException("The operation has timed out.");
43+
timeoutCancellationTokenSource.Cancel();
44+
return await task.ConfigureAwait(false);
5145
}
46+
47+
throw new TimeoutException("The operation has timed out.");
5248
}
5349
}

0 commit comments

Comments
 (0)