Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
mattiasnordqvist committed Oct 4, 2023
1 parent 6ea8574 commit 87b582f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Results/DotNetThoughts.Results.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Version>1.3.0</Version>
<Version>1.4.0</Version>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
</PropertyGroup>
Expand Down
30 changes: 30 additions & 0 deletions Results/Tap.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
namespace DotNetThoughts.Results;
public static partial class Extensions
{
/// <summary>
/// Tap into the result and perform an action if the result is successful.
/// </summary>
public static Result<T> Tap<T>(this Result<T> source, Action<T> next)
{
if (source.Success)
Expand All @@ -10,12 +13,39 @@ public static Result<T> Tap<T>(this Result<T> source, Action<T> next)
return source;
}

/// <summary>
/// Tap into the result and perform an action if the result is successful.
/// </summary>
public static async Task<Result<T>> Tap<T>(this Task<Result<T>> source, Action<T> next)
{
if ((await source).Success)
{
next((await source).Value);
}
return await source;
}

/// <summary>
/// Tap into the result and perform an action if the result is successful.
/// </summary>
public static async Task<Result<T>> Tap<T>(this Task<Result<T>> source, Func<T, Task> next)
{
if ((await source).Success)
{
await next((await source).Value);
}
return await source;
}

/// <summary>
/// Tap into the result and perform an action if the result is successful.
/// </summary>
public static async Task<Result<T>> Tap<T>(this Result<T> source, Func<T, Task> next)
{
if (source.Success)
{
await next(source.Value);
}
return source;
}
}

0 comments on commit 87b582f

Please sign in to comment.