I have this async function that returns Task
public async Task<SettingModel> GetSetting(string key) { var rootPath = _hostingEnvironment.ContentRootPath; using (StreamReader r = new StreamReader(rootPath + key + "settings.json")) { string json = await r.ReadToEndAsync(); var settings = JsonConvert.DeserializeObject<SettingModel>(json); return settings; } }
Now I want to get all the settings, and then wait until everything is complete, like this
public async Task GetData(List<string> keys) { var taskList = new List<Task>(); foreach(var key in keys) { taskList.Add(GetSetting(key)); } await Task.WhenAll(taskList.ToList()); foreach (var task in taskList) { task.Result
How to get data from a task?
Nigor source share