I am trying to make sure I understand async/await. In the following example, will my code run asynchronously or synchronously?
My understanding, which may be wrong, is that each async database call will not have to wait on the previous call to finish. So, I could essentially run numerous CountAsync
calls, and they would all run at the same time until at which point something tries to get data from one of the async calls.
Here is what I currently have: (All the select/where logic has been removed because it's just not needed for this question)
public async Task<DashboardModel> GetDashboard(DashboardInput input)
{
DashboardModel model = new DashboardModel();
model.MyCustomers = await _context.Customers.Where(x => [...]).Select(x => new DashboardCustomerModel()
{
[...]
}).ToListAsync();
model.TotalCustomers = await _context.Customers.CountAsync(x => [...]);
model.MyTotalCustomers = await _context.Customers.CountAsync(x => [...]);
model.MyClosedCustomers = await _context.Customers.CountAsync(x => [...]);
model.MyNotStartedCustomers = await _context.Customers.CountAsync(x => [...]);
model.OtherTotalCustomers = await _context.Customers.CountAsync(x => [...]);
model.OtherClosedCustomers = await _context.Customers.CountAsync(x => [...]);
model.OtherNotStartedCustomers = await _context.Customers.CountAsync(x => [...]);
model.PreparerApprovedCustomers = await _context.Customers.CountAsync(x => [...]);
model.ReviewerApprovedCustomers = await _context.Customers.CountAsync(x => [...]);
model.ApprovedCustomers = await _context.Customers.CountAsync(x => [...]);
return model;
}
My colleague states that this is not correct and all the calls will run synchronously; Hence the reason I am asking this question. If I am wrong then what is the proper way to write this method so that all the async calls run at the same time?