Your method is async, but may not be making the best use of resources.
What your method will do is enter the foreach
loop, create the MyRequestAsync
object, and then (at the await
point) it will give up its thread until the result becomes available. Once the result is available, an appropriate thread will be found and the method will resume running. It will add the result to list
and go back to the top of the loop, and repeat this whole process over and over.
But, consider this - if these requests are independent, you could, instead, make each of the requests in parallel, and then only continue running your method once all of the requests are complete. That would be something like:
public async Task<IEnumerable<Model>> Get([FromUri]IList<string> links)
{
IList<Task<Model>> list = new List<Task<Model>>();
foreach (var link in links)
{
MyRequestAsync request = new MyRequestAsync(link);
list.Add(request.GetResult());
}
return new List<Model>(await Task.WhenAll(list));
//Or just
//return await Task.WhenAll(list);
//Since we don't need to return a list
}
And, for silly points, you could re-write the whole method as:
return await Task.WhenAll(from l in links select new RequestAsync(l).GetResult());
But that may make it less readable.