Skip to content

Commit

Permalink
Added AwaitAsync extension method
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaoticz committed May 24, 2024
1 parent e4d6641 commit 90591e2
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Kotz.Extensions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ Defines the following extension methods:
- Trim: Removes all leading and trailing instances of a character from the current string builder.
- TrimEnd: Removes all trailing instances of a character from the current string builder.
- TrimStart: Removes all leading instances of a character from the current string builder.
- **Task Extensions**
- Executes the current task asynchronously and safely returns it.

Defines the following types:

Expand Down
37 changes: 37 additions & 0 deletions Kotz.Extensions/TaskExt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,41 @@ public static async Task<T> WhenAnyAsync<T>(this IEnumerable<Task<T>> collection
ArgumentNullException.ThrowIfNull(collection, nameof(collection));
return await (await Task.WhenAny(collection).ConfigureAwait(false)).ConfigureAwait(false);
}

/// <summary>
/// Executes this task asynchronously and safely returns the awaited task.
/// </summary>
/// <param name="task">This task.</param>
/// <returns>This awaited task.</returns>
public static async Task<Task> AwaitAsync(this Task task)
{
try
{
await task.ConfigureAwait(false);
return task;
}
catch
{
return task;
}
}

/// <summary>
/// Executes this task asynchronously and safely returns the awaited task.
/// </summary>
/// <typeparam name="T">Data type held by <paramref name="task"/>.</typeparam>
/// <param name="task">This task.</param>
/// <returns>This awaited task.</returns>
public static async Task<Task<T>> AwaitAsync<T>(this Task<T> task)
{
try
{
await task.ConfigureAwait(false);
return task;
}
catch
{
return task;
}
}
}
56 changes: 56 additions & 0 deletions Kotz.Tests/Extensions/TaskTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
namespace Kotz.Tests.Extensions;

public sealed class TaskTests
{
[Theory]
[InlineData(true)]
[InlineData(false)]
internal async Task AwaitAsyncTestAsync(bool succeed)
{
var originalTask = DelayAsync(TimeSpan.FromMilliseconds(100), succeed);
var awaitedTask = await originalTask.AwaitAsync();

Assert.Same(originalTask, awaitedTask);
}

[Theory]
[InlineData(true)]
[InlineData(false)]
internal async Task AwaitAsyncGenericTestAsync(bool succeed)
{
var originalTask = DelayAsync(10, TimeSpan.FromMilliseconds(100), succeed);
var awaitedTask = await originalTask.AwaitAsync();

Assert.Same(originalTask, awaitedTask);
}

/// <summary>
/// Asynchronously wait for the specified amount of time.
/// </summary>
/// <param name="timeSpan">The time to wait for.</param>
/// <param name="succeed">Defines whether an exception should be thrown after the wait time has elapsed.</param>
/// <exception cref="InvalidOperationException" />
private static async Task DelayAsync(TimeSpan timeSpan, bool succeed)
{
await Task.Delay(timeSpan);

if (!succeed)
throw new InvalidOperationException("Mock operation has failed.");
}

/// <summary>
/// Asynchronously wait for the specified amount of time.
/// </summary>
/// <param name="toReturn">The value to return from the method.</param>
/// <param name="timeSpan">The time to wait for.</param>
/// <param name="succeed">Defines whether an exception should be thrown after the wait time has elapsed.</param>
/// <exception cref="InvalidOperationException" />
private static async Task<T> DelayAsync<T>(T toReturn, TimeSpan timeSpan, bool succeed)
{
await Task.Delay(timeSpan);

return (succeed)
? toReturn
: throw new InvalidOperationException("Mock operation has failed.");
}
}

0 comments on commit 90591e2

Please # to comment.