From 1a8737ffa5933cfa96b36eb9d56fc9c41492ea26 Mon Sep 17 00:00:00 2001 From: Louis Seubert Date: Wed, 1 May 2024 17:48:11 +0200 Subject: [PATCH] feat: add join for list of `Task>` Allow joining `IEnumerable>>` to one single `Task>>` contains the success value of each result. --- .../Extensions/Extensions.Enumerable.cs | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/src/Result/Extensions/Extensions.Enumerable.cs b/src/Result/Extensions/Extensions.Enumerable.cs index b1791fb..cbaf3d8 100644 --- a/src/Result/Extensions/Extensions.Enumerable.cs +++ b/src/Result/Extensions/Extensions.Enumerable.cs @@ -34,4 +34,54 @@ public static partial class Extensions return list; } + + /// + /// + /// For parallel execution of the async tasks, one should await the Task.WhenAll() of the provided list + /// before calling this function + /// + /// + // ReSharper disable once InconsistentNaming + public static async Task>> Join(this IEnumerable>> results) + { + _ = results.TryGetNonEnumeratedCount(out var count); + var list = new List(count); + + foreach (var result in results) + { + if (!(await result).TryGetValue(out var value, out var error)) + { + return new Result>(error); + } + + list.Add(value); + } + + return list; + } + + /// + /// + /// For parallel execution of the async tasks, one should await the Task.WhenAll() of the provided list + /// before calling this function + /// + /// + // ReSharper disable once InconsistentNaming + public static async ValueTask>> Join(this IEnumerable>> results) + { + _ = results.TryGetNonEnumeratedCount(out var count); + var list = new List(count); + + foreach (var result in results) + { + if (!(await result).TryGetValue(out var value, out var error)) + { + return new Result>(error); + } + + list.Add(value); + } + + return list; + } } \ No newline at end of file