I have a loop that retrieves objects from a 3rd party API (hence why I have to ask for each object one at a time) and adds them to a list that I then return out of my procedure. It currently does these sequentially but I'd like the loop to be asynchronous to improve performance.
The basic code looks like this:
public async Task<List<ResponseObject<MyClass>>> GetMyClass(string[] references)
{
var responseObject = new ResponseObject<MyClass>();
var responseObjects = new List<ResponseObject<MyClass>>();
foreach (var reference in references)
{
responseObject = await GetExternalData(reference);
responseObjects.Add(responseObject);
}
return responseObjects;
}
The method I call is defined as this:
public async Task<ResponseObject<MyClass>> GetExternalData(string reference)
How do I need to change this to get it to turn the same list of ReponseObjects, having loaded them in parallel rather than sequentially? Any help would be gratefully received.