[C#] await inside loop should be a code smell

I would like to suggest a new code smell: the await inside a regular loop.

I believe that this could be refactor in several ways, both maintaining the (possibly) desired “relief of load” on the other end AND allowing parallel execution

A non compliant code would be:

foreach(var uri in listOfUri)
    await DoSomething(uri);

a possible refactor would be:

List<Task> tasks = new List<Task>();
foreach(var uri in listOfUri)
    tasks.Add(DoSomething(uri));
await Task.WhenAll(tasks);

There are other refactors that would allow sequential download (via recursion for example) and batching can be achieved using Parallel.Foreach and other means.

Refer to New Code Smell Suggestion: await inside loops for more details

thanks @Leonardo-Ferreira , we’re going to discuss it internally