rsdn / CodeJam

Set of handy reusable .NET components that can simplify your daily work and save your time when you copy and paste your favorite helper methods and classes from one project to another
MIT License
258 stars 35 forks source link

The order of elements in ForEachAsync return value is different from the order in source sequence #145

Closed ivan-menshikov closed 2 years ago

ivan-menshikov commented 2 years ago

One would expect ForEachAsync to behave like Task.WhenAll and return the results in the same order as the source sequence. This should at least be mentioned in the docs.

NN--- commented 2 years ago

Does .NET Parallel.ForEachAsync behave the same ?

https://docs.microsoft.com/en-us/dotnet/api/system.threading.tasks.parallel.foreachasync?view=net-6.0

ivan-menshikov commented 2 years ago

AFAIK there is no overload of Parallel.ForEachAsync returning Task<T[]> https://github.com/dotnet/runtime/blob/53d1d2c6ec322fd5eb3351982107d389c182ad46/src/libraries/System.Threading.Tasks.Parallel/src/System/Threading/Tasks/Parallel.ForEachAsync.cs

NN--- commented 2 years ago

I see, we are talking about public static async Task<TResult[]> ForEachAsync<T, TResult>.

If I understand correctly instead of ConcurrentQueue, we can use an array: https://github.com/rsdn/CodeJam/blob/master/CodeJam.Main/Threading/TaskHelper.ForEachAsync.cs#L148

WDYT ?