diff --git a/src/TaskExtensionsIfFulfilled.cs b/src/TaskExtensionsIfFulfilled.cs
index 91426c0..922cd77 100644
--- a/src/TaskExtensionsIfFulfilled.cs
+++ b/src/TaskExtensionsIfFulfilled.cs
@@ -10,20 +10,20 @@ public static partial class TaskExtensions
///
/// The task's underlying type.
/// The task.
- /// The action to perform if the task is fulfilled.
+ /// The action to perform if the task is fulfilled.
/// The task.
- public static Task IfFulfilled(this Task task, Action consumer)
- => task.ResultMap(TaskStatics.Tap(consumer));
+ public static Task IfFulfilled(this Task task, Action onFulfilled)
+ => task.ResultMap(TaskStatics.Tap(onFulfilled));
///
/// Executes a function and throws away the result if the is in a fulfilled state.
///
/// The task's underlying type.
- /// The type of the discarded result of .
+ /// The type of the discarded result of .
/// The task.
- /// The function to execute if the task is fulfilled.
+ /// The function to execute if the task is fulfilled.
/// The task.
- public static Task IfFulfilled(this Task task, Func> func)
+ public static Task IfFulfilled(this Task task, Func> onFulfilled)
=> task.ContinueWith(continuationTask =>
{
if (continuationTask.IsFaulted || continuationTask.IsCanceled)
@@ -31,11 +31,7 @@ public static Task IfFulfilled(this Task task, Func> func
return continuationTask;
}
- return continuationTask.Then(value =>
- {
- func(value);
- return value;
- });
+ return continuationTask.Then(value => onFulfilled(value).Then(_ => value));
}).Unwrap();
///
@@ -43,20 +39,8 @@ public static Task IfFulfilled(this Task task, Func> func
///
/// The task's underlying type.
/// The task.
- /// The function to execute if the task is fulfilled.
+ /// The function to execute if the task is fulfilled.
/// The task.
- public static Task IfFulfilled(this Task task, Func 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 IfFulfilled(this Task task, Func onFulfilled)
+ => task.IfFulfilled(value => onFulfilled(value).ContinueWith(_ => value));
}
diff --git a/tests/unit/IfFaulted/WithFullTaskFunc.cs b/tests/unit/IfFaulted/WithFullTaskFunc.cs
index 87c625f..9a77e38 100644
--- a/tests/unit/IfFaulted/WithFullTaskFunc.cs
+++ b/tests/unit/IfFaulted/WithFullTaskFunc.cs
@@ -23,7 +23,7 @@ public async Task ItShouldPerformASideEffect()
await Task.FromException(new ArgumentNullException())
.IfFaulted(onFaulted);
}
- catch (ArgumentNullException exception)
+ catch (ArgumentNullException)
{
}
@@ -154,4 +154,29 @@ public async Task ItShouldResultInAFaultedTaskWithAnException()
await Assert.ThrowsAsync(() => testTask);
}
+
+ [Fact]
+ public async Task ItShouldProperlyCaptureTheTask()
+ {
+ int actualValue = 0;
+ int expectedValue = 5;
+ Func> onFaulted = async _ =>
+ {
+ await Task.Delay(TimeSpan.FromSeconds(1));
+
+ actualValue = 5;
+ return 5;
+ };
+
+ try
+ {
+ await Task.FromException(new ArgumentNullException())
+ .IfFaulted(onFaulted);
+ }
+ catch (ArgumentNullException)
+ {
+ }
+
+ Assert.Equal(expectedValue, actualValue);
+ }
}
\ No newline at end of file
diff --git a/tests/unit/IfFaulted/WithRawTaskFunc.cs b/tests/unit/IfFaulted/WithRawTaskFunc.cs
index e2d643a..2e262e1 100644
--- a/tests/unit/IfFaulted/WithRawTaskFunc.cs
+++ b/tests/unit/IfFaulted/WithRawTaskFunc.cs
@@ -24,7 +24,7 @@ public async Task ItShouldPerformASideEffect()
await Task.FromException(new ArgumentNullException())
.IfFaulted(onFaulted);
}
- catch (ArgumentNullException exception)
+ catch (ArgumentNullException)
{
}
@@ -155,4 +155,28 @@ public async Task ItShouldResultInAFaultedTaskWithAnException()
await Assert.ThrowsAsync(() => testTask);
}
+
+ [Fact]
+ public async Task ItShouldProperlyCaptureTheTask()
+ {
+ int actualValue = 0;
+ int expectedValue = 5;
+ Func onFaulted = async _ =>
+ {
+ await Task.Delay(TimeSpan.FromSeconds(1));
+
+ actualValue = 5;
+ };
+
+ try
+ {
+ await Task.FromException(new ArgumentNullException())
+ .IfFaulted(onFaulted);
+ }
+ catch (ArgumentNullException)
+ {
+ }
+
+ Assert.Equal(expectedValue, actualValue);
+ }
}
\ No newline at end of file
diff --git a/tests/unit/IfFulfilled/WithFullTaskFunc.cs b/tests/unit/IfFulfilled/WithFullTaskFunc.cs
index e2acbf7..e148e0b 100644
--- a/tests/unit/IfFulfilled/WithFullTaskFunc.cs
+++ b/tests/unit/IfFulfilled/WithFullTaskFunc.cs
@@ -141,4 +141,23 @@ public async Task ItShouldResultInAFaultedTaskWithAnException()
await Assert.ThrowsAsync(() => testTask);
}
+
+ [Fact]
+ public async Task ItShouldProperlyCaptureTheTask()
+ {
+ int actualValue = 0;
+ int expectedValue = 5;
+ Func> onFulfilled = async _ =>
+ {
+ await Task.Delay(TimeSpan.FromSeconds(1));
+
+ actualValue = 5;
+ return 5;
+ };
+
+ await Task.FromResult(5)
+ .IfFulfilled(onFulfilled);
+
+ Assert.Equal(expectedValue, actualValue);
+ }
}
\ No newline at end of file
diff --git a/tests/unit/IfFulfilled/WithRawTaskFunc.cs b/tests/unit/IfFulfilled/WithRawTaskFunc.cs
index c3d1e38..931c6c3 100644
--- a/tests/unit/IfFulfilled/WithRawTaskFunc.cs
+++ b/tests/unit/IfFulfilled/WithRawTaskFunc.cs
@@ -141,4 +141,22 @@ public async Task ItShouldResultInAFaultedTaskWithAnException()
await Assert.ThrowsAsync(() => testTask);
}
+
+ [Fact]
+ public async Task ItShouldProperlyCaptureTheTask()
+ {
+ int actualValue = 0;
+ int expectedValue = 5;
+ Func onFulfilled = async _ =>
+ {
+ await Task.Delay(TimeSpan.FromSeconds(1));
+
+ actualValue = 5;
+ };
+
+ await Task.FromResult(5)
+ .IfFulfilled(onFulfilled);
+
+ Assert.Equal(expectedValue, actualValue);
+ }
}
\ No newline at end of file