Skip to main content
added 11 characters in body
Source Link
Ilya Chernomordik
  • 30.9k
  • 31
  • 148
  • 245

I have the following scenario:

Customer has a number of accounts, each of them has a number of cards attached.

Now I have a request where I need to query accounts with cards on multiple customers. I have async methods to query accounts and cards separately, i.e. FindAccounts(string customer), FindCards(string[] accounts).

So I have this method:

public async Task<Cards[]>Task<Data> FindCustomersWithCards(string[] customers) { var accountsTasks = customers.Select(_service.FindAccounts); var accounts = await Task.WhenAll(accountsTasks); var cardsTasks = accounts.Select(_service.FindCards); var cards = await Tasks.WhenAll(cardsTasks)    ... } 

While this will work it has a problem that you have to wait for accounts of all customers to be finished before cards can be queried. A more efficient implementation would go on and query cards for customer accounts as soon as accounts querying for a particular customer is finished (without waiting for other customers).

My question is if it is possible to do this with async/await. I think I can manage with ContinueWith, but I am not 100% sure it is OK to mix async/await with ContinueWith approach.

I have the following scenario:

Customer has a number of accounts, each of them has a number of cards attached.

Now I have a request where I need to query accounts with cards on multiple customers. I have async methods to query accounts and cards separately, i.e. FindAccounts(string customer), FindCards(string[] accounts).

So I have this method:

public async Task<Cards[]> FindCustomersWithCards(string[] customers) { var accountsTasks = customers.Select(_service.FindAccounts); var accounts = await Task.WhenAll(accountsTasks); var cardsTasks = accounts.Select(_service.FindCards); var cards = await Tasks.WhenAll(cardsTasks) } 

While this will work it has a problem that you have to wait for accounts of all customers to be finished before cards can be queried. A more efficient implementation would go on and query cards for customer accounts as soon as accounts querying for a particular customer is finished (without waiting for other customers).

My question is if it is possible to do this with async/await. I think I can manage with ContinueWith, but I am not 100% sure it is OK to mix async/await with ContinueWith approach.

I have the following scenario:

Customer has a number of accounts, each of them has a number of cards attached.

Now I have a request where I need to query accounts with cards on multiple customers. I have async methods to query accounts and cards separately, i.e. FindAccounts(string customer), FindCards(string[] accounts).

So I have this method:

public async Task<Data> FindCustomersWithCards(string[] customers) { var accountsTasks = customers.Select(_service.FindAccounts); var accounts = await Task.WhenAll(accountsTasks); var cardsTasks = accounts.Select(_service.FindCards); var cards = await Tasks.WhenAll(cardsTasks)    ... } 

While this will work it has a problem that you have to wait for accounts of all customers to be finished before cards can be queried. A more efficient implementation would go on and query cards for customer accounts as soon as accounts querying for a particular customer is finished (without waiting for other customers).

My question is if it is possible to do this with async/await. I think I can manage with ContinueWith, but I am not 100% sure it is OK to mix async/await with ContinueWith approach.

Source Link
Ilya Chernomordik
  • 30.9k
  • 31
  • 148
  • 245

How to use async/await in scenario with multiple tasks with each task requiring a new async/await

I have the following scenario:

Customer has a number of accounts, each of them has a number of cards attached.

Now I have a request where I need to query accounts with cards on multiple customers. I have async methods to query accounts and cards separately, i.e. FindAccounts(string customer), FindCards(string[] accounts).

So I have this method:

public async Task<Cards[]> FindCustomersWithCards(string[] customers) { var accountsTasks = customers.Select(_service.FindAccounts); var accounts = await Task.WhenAll(accountsTasks); var cardsTasks = accounts.Select(_service.FindCards); var cards = await Tasks.WhenAll(cardsTasks) } 

While this will work it has a problem that you have to wait for accounts of all customers to be finished before cards can be queried. A more efficient implementation would go on and query cards for customer accounts as soon as accounts querying for a particular customer is finished (without waiting for other customers).

My question is if it is possible to do this with async/await. I think I can manage with ContinueWith, but I am not 100% sure it is OK to mix async/await with ContinueWith approach.