I have an async function where I must make an async call for each of the elements of a list. To do this, I have written this piece of code:
List<string> batchItems;
batchItems.ForEach(async t => await SubmitBatchItemAsync(input, t));
However, this is not working: the SubmitBatchItemAsync is invoked, but it is not awaited.
I had to change this code to this to make it work:
List<string> batchItems;
foreach (var batchItem in batchItems)
{
await SubmitBatchItemAsync(input, batchItem);
}
This also works, but it is not exactly the same, as Task.Wait() does not work exactly as await does:
List<string> batchItems;
batchItems.ForEach(t => SubmitBatchItemAsync(input, t).Wait(CancellationToken.None));
Does anyone know why the first option is not working, as LINQ expressions support await? ( https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/statements-expressions-operators/lambda-expressions#async-lambdas )