Skip to content

Commit a5a938c

Browse files
feat: add ToAsyncEnumerable
1 parent b1a3dc1 commit a5a938c

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

src/WouterVanRanst.Utils/Extensions/IEnumerableExtensions.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace WouterVanRanst.Utils.Extensions;
1+
using System.Runtime.CompilerServices;
2+
3+
namespace WouterVanRanst.Utils.Extensions;
24

35
public static class IEnumerableExtensions
46
{
@@ -72,4 +74,16 @@ public static IEnumerable<T> DuplicatesBy<T, TKey>(this IEnumerable<T> collectio
7274
.Where(group => group.Count() > 1)
7375
.SelectMany(group => group);
7476
}
77+
78+
public static async IAsyncEnumerable<T> ToAsyncEnumerable<T>(this IEnumerable<T> source, [EnumeratorCancellation] CancellationToken cancellationToken = default)
79+
{
80+
foreach (var item in source)
81+
{
82+
cancellationToken.ThrowIfCancellationRequested();
83+
yield return item;
84+
85+
// Give the scheduler a chance to run other work; keeps it truly async-friendly.
86+
await Task.Yield();
87+
}
88+
}
7589
}

0 commit comments

Comments
 (0)