Skip to content

Commit

Permalink
xforeachasync 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
nameofSEOKWONHONG committed Aug 1, 2024
1 parent 459ee29 commit 905f680
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 25 deletions.
17 changes: 4 additions & 13 deletions src/XForEachExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,24 +84,15 @@ public static void xForEach<T>(this IEnumerable<T> iterator, Func<int, T, bool>
public static async Task xForEachAsync<T>(this IEnumerable<T> items, Func<T, Task> func)
{
if (items.xIsEmpty()) return;
var results = new List<Task>();

foreach (var value in items)
{
await func(value);
results.Add(func(value));
}
}

public static async Task xForEachAsync<T>(this IEnumerable<T> items, Func<int, T, Task> func)
{
if (items.xIsEmpty()) return;

int i = 0;
foreach (var value in items)
{
await func(i, value);
i++;
}
}
await Task.WhenAll(results);
}

public static void xForEach(this ValueTuple<DateTime, DateTime> dateRange, Action<DateTime> action)
{
Expand Down
22 changes: 10 additions & 12 deletions test/xForEachTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,17 @@ public void xforeach_test()
[Test]
public async Task xforeach_async_test()
{
var ranges = Enumerable.Range(1, 5001).ToList();
var isFind = false;
await ranges.xForEachAsync(async item =>
{
await Task.Run(() => {
if (isFind.xIsFalse())
{
isFind = item == 2000;
}
});
var ranges = Enumerable.Range(1, 100).ToList();
var value = 0;

Check warning on line 32 in test/xForEachTest.cs

View workflow job for this annotation

GitHub Actions / build

The variable 'value' is assigned but its value is never used

Check warning on line 32 in test/xForEachTest.cs

View workflow job for this annotation

GitHub Actions / build

The variable 'value' is assigned but its value is never used
await ranges.xForEachAsync(Process);
//Assert.That(value, Is.Not.Zero);
}

});
Assert.IsTrue(isFind);
private async Task Process(int i)
{
TestContext.WriteLine(i);
await Task.Delay(1);
TestContext.WriteLine(i + "complete");
}

[Test]
Expand Down

0 comments on commit 905f680

Please sign in to comment.