await not working foreach code example
Example 1: async foreach
[1, 2, 3].forEach(async (num) => { await waitFor(50); console.log(num);});console.log('Done');
Example 2: C# foreach loop async but wait at end
public async Task RunAsync()
{
var tasks = new List<Task>();
foreach (var x in new[] { 1, 2, 3 })
{
var task = DoSomethingAsync(x);
tasks.Add(task);
}
await Task.WhenAll();
}