Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 10 additions & 26 deletions src/TaskExtensionsIfFulfilled.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,53 +10,37 @@ public static partial class TaskExtensions
/// </summary>
/// <typeparam name="T">The task's underlying type.</typeparam>
/// <param name="task">The task.</param>
/// <param name="consumer">The action to perform if the task is fulfilled.</param>
/// <param name="onFulfilled">The action to perform if the task is fulfilled.</param>
/// <returns>The task.</returns>
public static Task<T> IfFulfilled<T>(this Task<T> task, Action<T> consumer)
=> task.ResultMap(TaskStatics.Tap(consumer));
public static Task<T> IfFulfilled<T>(this Task<T> task, Action<T> onFulfilled)
=> task.ResultMap(TaskStatics.Tap(onFulfilled));

/// <summary>
/// Executes a function and throws away the result if the <see name="Task{T}"/> is in a fulfilled state.
/// </summary>
/// <typeparam name="T">The task's underlying type.</typeparam>
/// <typeparam name="R">The type of the discarded result of <paramref name="func"/>.</typeparam>
/// <typeparam name="R">The type of the discarded result of <paramref name="onFulfilled"/>.</typeparam>
/// <param name="task">The task.</param>
/// <param name="func">The function to execute if the task is fulfilled.</param>
/// <param name="onFulfilled">The function to execute if the task is fulfilled.</param>
/// <returns>The task.</returns>
public static Task<T> IfFulfilled<T, R>(this Task<T> task, Func<T, Task<R>> func)
public static Task<T> IfFulfilled<T, R>(this Task<T> task, Func<T, Task<R>> onFulfilled)
=> task.ContinueWith(continuationTask =>
{
if (continuationTask.IsFaulted || continuationTask.IsCanceled)
{
return continuationTask;
}

return continuationTask.Then(value =>
{
func(value);
return value;
});
return continuationTask.Then(value => onFulfilled(value).Then(_ => value));
}).Unwrap();

/// <summary>
/// Executes a function and throws away the result if the <see name="Task{T}"/> is in a fulfilled state.
/// </summary>
/// <typeparam name="T">The task's underlying type.</typeparam>
/// <param name="task">The task.</param>
/// <param name="func">The function to execute if the task is fulfilled.</param>
/// <param name="onFulfilled">The function to execute if the task is fulfilled.</param>
/// <returns>The task.</returns>
public static Task<T> IfFulfilled<T>(this Task<T> task, Func<T, Task> func)
=> task.ContinueWith(continuationTask =>
{
if (continuationTask.IsFaulted || continuationTask.IsCanceled)
{
return continuationTask;
}

return continuationTask.Then(async value =>
{
await func(value);
return value;
});
}).Unwrap();
public static Task<T> IfFulfilled<T>(this Task<T> task, Func<T, Task> onFulfilled)
=> task.IfFulfilled(value => onFulfilled(value).ContinueWith(_ => value));
}
27 changes: 26 additions & 1 deletion tests/unit/IfFaulted/WithFullTaskFunc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public async Task ItShouldPerformASideEffect()
await Task.FromException<int>(new ArgumentNullException())
.IfFaulted(onFaulted);
}
catch (ArgumentNullException exception)
catch (ArgumentNullException)
{
}

Expand Down Expand Up @@ -154,4 +154,29 @@ public async Task ItShouldResultInAFaultedTaskWithAnException()

await Assert.ThrowsAsync<ArgumentNullException>(() => testTask);
}

[Fact]
public async Task ItShouldProperlyCaptureTheTask()
{
int actualValue = 0;
int expectedValue = 5;
Func<Exception, Task<int>> onFaulted = async _ =>
{
await Task.Delay(TimeSpan.FromSeconds(1));

actualValue = 5;
return 5;
};

try
{
await Task.FromException<int>(new ArgumentNullException())
.IfFaulted(onFaulted);
}
catch (ArgumentNullException)
{
}

Assert.Equal(expectedValue, actualValue);
}
}
26 changes: 25 additions & 1 deletion tests/unit/IfFaulted/WithRawTaskFunc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public async Task ItShouldPerformASideEffect()
await Task.FromException<int>(new ArgumentNullException())
.IfFaulted(onFaulted);
}
catch (ArgumentNullException exception)
catch (ArgumentNullException)
{
}

Expand Down Expand Up @@ -155,4 +155,28 @@ public async Task ItShouldResultInAFaultedTaskWithAnException()

await Assert.ThrowsAsync<ArgumentNullException>(() => testTask);
}

[Fact]
public async Task ItShouldProperlyCaptureTheTask()
{
int actualValue = 0;
int expectedValue = 5;
Func<Exception, Task> onFaulted = async _ =>
{
await Task.Delay(TimeSpan.FromSeconds(1));

actualValue = 5;
};

try
{
await Task.FromException<int>(new ArgumentNullException())
.IfFaulted(onFaulted);
}
catch (ArgumentNullException)
{
}

Assert.Equal(expectedValue, actualValue);
}
}
19 changes: 19 additions & 0 deletions tests/unit/IfFulfilled/WithFullTaskFunc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,23 @@ public async Task ItShouldResultInAFaultedTaskWithAnException()

await Assert.ThrowsAsync<ArgumentNullException>(() => testTask);
}

[Fact]
public async Task ItShouldProperlyCaptureTheTask()
{
int actualValue = 0;
int expectedValue = 5;
Func<int, Task<int>> onFulfilled = async _ =>
{
await Task.Delay(TimeSpan.FromSeconds(1));

actualValue = 5;
return 5;
};

await Task.FromResult(5)
.IfFulfilled(onFulfilled);

Assert.Equal(expectedValue, actualValue);
}
}
18 changes: 18 additions & 0 deletions tests/unit/IfFulfilled/WithRawTaskFunc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,22 @@ public async Task ItShouldResultInAFaultedTaskWithAnException()

await Assert.ThrowsAsync<ArgumentNullException>(() => testTask);
}

[Fact]
public async Task ItShouldProperlyCaptureTheTask()
{
int actualValue = 0;
int expectedValue = 5;
Func<int, Task> onFulfilled = async _ =>
{
await Task.Delay(TimeSpan.FromSeconds(1));

actualValue = 5;
};

await Task.FromResult(5)
.IfFulfilled(onFulfilled);

Assert.Equal(expectedValue, actualValue);
}
}