From 7f7262fb96daf0293e1f3ab0d5246501fad497be Mon Sep 17 00:00:00 2001 From: dpraimeyuu Date: Wed, 3 Jun 2020 14:21:25 +0200 Subject: [PATCH] Add OnSuccessTry returning Task --- .../ResultTests/ResultTests.cs | 30 +++++++++++++++++++ .../Extensions/OnSuccessTryAsyncLeft.cs | 16 ++++++++++ 2 files changed, 46 insertions(+) diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/ResultTests.cs b/CSharpFunctionalExtensions.Tests/ResultTests/ResultTests.cs index f592da6e..38b1c56a 100644 --- a/CSharpFunctionalExtensions.Tests/ResultTests/ResultTests.cs +++ b/CSharpFunctionalExtensions.Tests/ResultTests/ResultTests.cs @@ -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(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(content); + + await result + .OnSuccessTry(async (key) => + { + await Task.Delay(10); + var task = Task.FromResult(key); + var k = await task; + k.Should().Be(content); + }); + } } } \ No newline at end of file diff --git a/CSharpFunctionalExtensions/Result/Extensions/OnSuccessTryAsyncLeft.cs b/CSharpFunctionalExtensions/Result/Extensions/OnSuccessTryAsyncLeft.cs index 07858477..d23d86ce 100644 --- a/CSharpFunctionalExtensions/Result/Extensions/OnSuccessTryAsyncLeft.cs +++ b/CSharpFunctionalExtensions/Result/Extensions/OnSuccessTryAsyncLeft.cs @@ -32,5 +32,21 @@ public static async Task> OnSuccessTry(this Task> task var result = await task.DefaultAwait(); return result.OnSuccessTry(action, errorHandler); } + + public static async Task OnSuccessTry(this Result result, Func func) + { + if (result.IsSuccess) + { + await func(result.Value); + } + } + + public static async Task OnSuccessTry(this Result result, Func func) + { + if (result.IsSuccess) + { + await func(result.Value); + } + } } }