Skip to content

Commit

Permalink
Merge pull request #206 from dpraimeyuu/fix-on-success-try-async
Browse files Browse the repository at this point in the history
Add OnSuccessTry returning Task
  • Loading branch information
vkhorikov authored Jun 3, 2020
2 parents ff7bc0d + 7f7262f commit 6435012
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
30 changes: 30 additions & 0 deletions CSharpFunctionalExtensions.Tests/ResultTests/ResultTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -674,5 +674,35 @@ public async Task Try_async_with_error_execute_function_failed_with_error_handle
private class Error
{
}

[Fact]
public async Task When_Asynchronous_Continuation_Passed_To_On_Success_Try_Then_Needs_To_Be_Handled_As_Awaitable() {
var content = "my key";
var result = Result.Success<string>(content);

await result
.OnSuccessTry(async (key) => {
await Task.Delay(10);
var task = Task.FromResult(key);
var k = await task;
k.Should().Be(content);
});
}

[Fact]
public async Task When_Asynchronous_Continuation_Passed_With_Typed_Error_To_On_Success_Try_Then_Needs_To_Be_Handled_As_Awaitable()
{
var content = "my key";
var result = Result.Success<string, Error>(content);

await result
.OnSuccessTry(async (key) =>
{
await Task.Delay(10);
var task = Task.FromResult(key);
var k = await task;
k.Should().Be(content);
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,21 @@ public static async Task<Result<K>> OnSuccessTry<T, K>(this Task<Result<T>> task
var result = await task.DefaultAwait();
return result.OnSuccessTry(action, errorHandler);
}

public static async Task OnSuccessTry<T>(this Result<T> result, Func<T, Task> func)
{
if (result.IsSuccess)
{
await func(result.Value);
}
}

public static async Task OnSuccessTry<T, E>(this Result<T, E> result, Func<T, Task> func)
{
if (result.IsSuccess)
{
await func(result.Value);
}
}
}
}

0 comments on commit 6435012

Please sign in to comment.