How can I optimise a foreach calling an API? The code calls an API and returns the first string reponse
one way of solving this would be using Task.WhenAll the idea is to add all tasks into a list of tasks then we call Task.WhenAll and await the final results.
Here is an example of it :
private async Task&lt;ProductItem&gt; SetSiteLink(ProductItem item)
{   
    item.site = await _testRepo.GetSiteLink(item.Id, item.baseLink); 
    
    return Task.FromResult(item);  
}

then call it like this :
var tasks = new List&lt;Task&lt;ProductItem&gt;&gt;();

foreach (var item in products)
{
    tasks.Add(SetSiteLink(item));
}

var results = await Task.WhenAll(tasks);