❔ Returning an async dictionary ?

I am calling this function from an api endpoint and want to return the results from the tasks. Right now I store all results in a dictionary but am returning a Task. Is there a better way to do this so that I just return the result as a plain dictionary instead of a Task ?
public class DataGatherer
{
static async Task<Dictionary<string, object>> GetData()
{
Dictionary<string, object> dict = new Dictionary<string, object>();

using HttpClient client = new();

try
{

var tasks = new Task[] {
Task.Run(async () => await ExternalTablesGatherer.ProcessRepositoriesAsync(client, "6382963", dict)),
Task.Run(async () => await QueueDataGatherer.GetDataAsync(client, "6010081", dict))
};

await Task.WhenAll(tasks);

return dict;

}
catch (Exception exception)
{
Console.WriteLine(exception);
return null;
}
}
}
public class DataGatherer
{
static async Task<Dictionary<string, object>> GetData()
{
Dictionary<string, object> dict = new Dictionary<string, object>();

using HttpClient client = new();

try
{

var tasks = new Task[] {
Task.Run(async () => await ExternalTablesGatherer.ProcessRepositoriesAsync(client, "6382963", dict)),
Task.Run(async () => await QueueDataGatherer.GetDataAsync(client, "6010081", dict))
};

await Task.WhenAll(tasks);

return dict;

}
catch (Exception exception)
{
Console.WriteLine(exception);
return null;
}
}
}
2 Replies
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.