When should I await my asyncs?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
40
down vote

favorite
14












We're currently refactoring sections of our project to be async up and down, yay!



Due to our different understanding, me and a colleague (let's call him Jim), have differing opinions about how our async/await code will execute, and which way to write it.



Here is the example method Jim wrote:



public async Task<HouseModel> GetHouseModel(Guid houseId)

House house = await _houseService.GetHouse(houseId);

Task<IEnumerable<Furniture>> furniture = _furnitureService.GetFurnitureForHouse(house);

Task<IEnumerable<Appliances>> appliances = _applianceService.GetAppliancesForHouse(house);

return _houseModelFactory.MakeHouseModel(await furniture, await appliances);



And the example of how I would write it:



public async Task<HouseModel> GetHouseModel(Guid houseId)

House house = await _houseService.GetHouse(houseId);

IEnumerable<Furniture> furniture = await _furnitureService.GetFurnitureForHouse(house);

IEnumerable<Appliances> appliances = await _applianceService.GetAppliancesForHouse(house);

return _houseModelFactory.MakeHouseModel(furniture, appliances);



My understanding is: because the methods in both the furniture and appliance services in the above require House, they will wait for House to be available before continuing. Then, both methods that need House will run, but the second method (GetAppliancesForHouse) will not wait for the first to finish before starting.



Jim's understanding is: that we should await both methods only when they are needed. So that they will both run parallel to each other. He thinks that doing it my way will result in the second method waiting for the first, i.e.: GetAppliancesForHouse waiting for GetFurnitureForHouse.



Are any of these understandings correct? Or have we just been making it up as we go along? When should we await?







share|improve this question






















  • Could you show us the source code for GetFurnitureForHouse?
    – mjwills
    Aug 22 at 12:09










  • If you don't await an async method it will return, and execution of the calling code will continute, as soon as it hits the first await in the async method.
    – mm8
    Aug 22 at 12:10






  • 16




    It's funny how this question and answers shows how misunderstood async/await is. When it's celebrated for how natural it is to previous methods.
    – Erndob
    Aug 22 at 12:24






  • 3




    Possible duplicate of multiple awaits vs Task.WaitAll - equivalent?
    – Dmitry Pavliv
    Aug 22 at 12:58






  • 4




    How is that question "primarily opinion-based"?
    – Ivanka Todorova
    Aug 22 at 16:29














up vote
40
down vote

favorite
14












We're currently refactoring sections of our project to be async up and down, yay!



Due to our different understanding, me and a colleague (let's call him Jim), have differing opinions about how our async/await code will execute, and which way to write it.



Here is the example method Jim wrote:



public async Task<HouseModel> GetHouseModel(Guid houseId)

House house = await _houseService.GetHouse(houseId);

Task<IEnumerable<Furniture>> furniture = _furnitureService.GetFurnitureForHouse(house);

Task<IEnumerable<Appliances>> appliances = _applianceService.GetAppliancesForHouse(house);

return _houseModelFactory.MakeHouseModel(await furniture, await appliances);



And the example of how I would write it:



public async Task<HouseModel> GetHouseModel(Guid houseId)

House house = await _houseService.GetHouse(houseId);

IEnumerable<Furniture> furniture = await _furnitureService.GetFurnitureForHouse(house);

IEnumerable<Appliances> appliances = await _applianceService.GetAppliancesForHouse(house);

return _houseModelFactory.MakeHouseModel(furniture, appliances);



My understanding is: because the methods in both the furniture and appliance services in the above require House, they will wait for House to be available before continuing. Then, both methods that need House will run, but the second method (GetAppliancesForHouse) will not wait for the first to finish before starting.



Jim's understanding is: that we should await both methods only when they are needed. So that they will both run parallel to each other. He thinks that doing it my way will result in the second method waiting for the first, i.e.: GetAppliancesForHouse waiting for GetFurnitureForHouse.



Are any of these understandings correct? Or have we just been making it up as we go along? When should we await?







share|improve this question






















  • Could you show us the source code for GetFurnitureForHouse?
    – mjwills
    Aug 22 at 12:09










  • If you don't await an async method it will return, and execution of the calling code will continute, as soon as it hits the first await in the async method.
    – mm8
    Aug 22 at 12:10






  • 16




    It's funny how this question and answers shows how misunderstood async/await is. When it's celebrated for how natural it is to previous methods.
    – Erndob
    Aug 22 at 12:24






  • 3




    Possible duplicate of multiple awaits vs Task.WaitAll - equivalent?
    – Dmitry Pavliv
    Aug 22 at 12:58






  • 4




    How is that question "primarily opinion-based"?
    – Ivanka Todorova
    Aug 22 at 16:29












up vote
40
down vote

favorite
14









up vote
40
down vote

favorite
14






14





We're currently refactoring sections of our project to be async up and down, yay!



Due to our different understanding, me and a colleague (let's call him Jim), have differing opinions about how our async/await code will execute, and which way to write it.



Here is the example method Jim wrote:



public async Task<HouseModel> GetHouseModel(Guid houseId)

House house = await _houseService.GetHouse(houseId);

Task<IEnumerable<Furniture>> furniture = _furnitureService.GetFurnitureForHouse(house);

Task<IEnumerable<Appliances>> appliances = _applianceService.GetAppliancesForHouse(house);

return _houseModelFactory.MakeHouseModel(await furniture, await appliances);



And the example of how I would write it:



public async Task<HouseModel> GetHouseModel(Guid houseId)

House house = await _houseService.GetHouse(houseId);

IEnumerable<Furniture> furniture = await _furnitureService.GetFurnitureForHouse(house);

IEnumerable<Appliances> appliances = await _applianceService.GetAppliancesForHouse(house);

return _houseModelFactory.MakeHouseModel(furniture, appliances);



My understanding is: because the methods in both the furniture and appliance services in the above require House, they will wait for House to be available before continuing. Then, both methods that need House will run, but the second method (GetAppliancesForHouse) will not wait for the first to finish before starting.



Jim's understanding is: that we should await both methods only when they are needed. So that they will both run parallel to each other. He thinks that doing it my way will result in the second method waiting for the first, i.e.: GetAppliancesForHouse waiting for GetFurnitureForHouse.



Are any of these understandings correct? Or have we just been making it up as we go along? When should we await?







share|improve this question














We're currently refactoring sections of our project to be async up and down, yay!



Due to our different understanding, me and a colleague (let's call him Jim), have differing opinions about how our async/await code will execute, and which way to write it.



Here is the example method Jim wrote:



public async Task<HouseModel> GetHouseModel(Guid houseId)

House house = await _houseService.GetHouse(houseId);

Task<IEnumerable<Furniture>> furniture = _furnitureService.GetFurnitureForHouse(house);

Task<IEnumerable<Appliances>> appliances = _applianceService.GetAppliancesForHouse(house);

return _houseModelFactory.MakeHouseModel(await furniture, await appliances);



And the example of how I would write it:



public async Task<HouseModel> GetHouseModel(Guid houseId)

House house = await _houseService.GetHouse(houseId);

IEnumerable<Furniture> furniture = await _furnitureService.GetFurnitureForHouse(house);

IEnumerable<Appliances> appliances = await _applianceService.GetAppliancesForHouse(house);

return _houseModelFactory.MakeHouseModel(furniture, appliances);



My understanding is: because the methods in both the furniture and appliance services in the above require House, they will wait for House to be available before continuing. Then, both methods that need House will run, but the second method (GetAppliancesForHouse) will not wait for the first to finish before starting.



Jim's understanding is: that we should await both methods only when they are needed. So that they will both run parallel to each other. He thinks that doing it my way will result in the second method waiting for the first, i.e.: GetAppliancesForHouse waiting for GetFurnitureForHouse.



Are any of these understandings correct? Or have we just been making it up as we go along? When should we await?









share|improve this question













share|improve this question




share|improve this question








edited Aug 25 at 7:19









Stephen Muecke

94.7k1993115




94.7k1993115










asked Aug 22 at 12:07









Jack Pettinger

2,1481531




2,1481531











  • Could you show us the source code for GetFurnitureForHouse?
    – mjwills
    Aug 22 at 12:09










  • If you don't await an async method it will return, and execution of the calling code will continute, as soon as it hits the first await in the async method.
    – mm8
    Aug 22 at 12:10






  • 16




    It's funny how this question and answers shows how misunderstood async/await is. When it's celebrated for how natural it is to previous methods.
    – Erndob
    Aug 22 at 12:24






  • 3




    Possible duplicate of multiple awaits vs Task.WaitAll - equivalent?
    – Dmitry Pavliv
    Aug 22 at 12:58






  • 4




    How is that question "primarily opinion-based"?
    – Ivanka Todorova
    Aug 22 at 16:29
















  • Could you show us the source code for GetFurnitureForHouse?
    – mjwills
    Aug 22 at 12:09










  • If you don't await an async method it will return, and execution of the calling code will continute, as soon as it hits the first await in the async method.
    – mm8
    Aug 22 at 12:10






  • 16




    It's funny how this question and answers shows how misunderstood async/await is. When it's celebrated for how natural it is to previous methods.
    – Erndob
    Aug 22 at 12:24






  • 3




    Possible duplicate of multiple awaits vs Task.WaitAll - equivalent?
    – Dmitry Pavliv
    Aug 22 at 12:58






  • 4




    How is that question "primarily opinion-based"?
    – Ivanka Todorova
    Aug 22 at 16:29















Could you show us the source code for GetFurnitureForHouse?
– mjwills
Aug 22 at 12:09




Could you show us the source code for GetFurnitureForHouse?
– mjwills
Aug 22 at 12:09












If you don't await an async method it will return, and execution of the calling code will continute, as soon as it hits the first await in the async method.
– mm8
Aug 22 at 12:10




If you don't await an async method it will return, and execution of the calling code will continute, as soon as it hits the first await in the async method.
– mm8
Aug 22 at 12:10




16




16




It's funny how this question and answers shows how misunderstood async/await is. When it's celebrated for how natural it is to previous methods.
– Erndob
Aug 22 at 12:24




It's funny how this question and answers shows how misunderstood async/await is. When it's celebrated for how natural it is to previous methods.
– Erndob
Aug 22 at 12:24




3




3




Possible duplicate of multiple awaits vs Task.WaitAll - equivalent?
– Dmitry Pavliv
Aug 22 at 12:58




Possible duplicate of multiple awaits vs Task.WaitAll - equivalent?
– Dmitry Pavliv
Aug 22 at 12:58




4




4




How is that question "primarily opinion-based"?
– Ivanka Todorova
Aug 22 at 16:29




How is that question "primarily opinion-based"?
– Ivanka Todorova
Aug 22 at 16:29












2 Answers
2






active

oldest

votes

















up vote
45
down vote



accepted











My understanding is: because the methods in both the furniture and appliance services in the above require House, they will wait for House to be available before continuing.




Your understanding is wrong. The methods that require House, they are not waiting for you to get House because you need it. They don't resolve their dependencies and when to wait for code or not on their own. The code waits to get Houses because you have await before it. It's not aware of what's going to happen next.




Then, both methods that need House will run, but the second method (GetAppliancesForHouse) will not wait for the first to finish before starting.




Similarly, the GetAppliancesForHouse won't have its own understanding if it should wait or not based on the dependencies. GetAppliancesForHouse won't run, because your code says to await GetFurnitureForHouse before it first. Your code will always run sequentially.




Jim's understanding is: that we should await both methods only when they are needed. So that they will both run parallel to each other.




That's generally true. As others have pointed out, the code still might run not in parallel depending on other factors. Also, there might be legitimate reasons to not want to run code in parallel.




He thinks that doing it my way will result in the second method waiting for the first, ie: GetAppliancesForHouse waiting for GetFurnitureForHouse.




He's right.



To see what happens exactly, you can put breakpoints and see what happens after each line.
In Jims case, after going from Furniture to Appliances, furniture variable won't have the value yet, it's still a task, but you are already in the next line.



With your case, going to Appliances line, you will see that Furniture already has the value, since it waited for it.






share|improve this answer


















  • 3




    A demonstration: pastebin.com/ZbLjH6qm
    – GSerg
    Aug 22 at 12:38






  • 4




    @CamiloTerevinto is right. In addition, I'd point out that Jim's way may introduce threading issues. E.g. if EF is used for data access and _applianceService and _furnitureService share the same DbContext instance, there'll be problems as DbContext is not thread-safe.
    – Adam Simon
    Aug 22 at 12:52







  • 5




    @AdamSimon one always has to consider potential threading issues, that's why it is important to understand how async/await works.
    – Crowcoder
    Aug 22 at 13:02










  • @Crowcoder Exactly. But there's so much confusion here about async/await, I considered it important to point out that this aspect must be taken into account, as well.
    – Adam Simon
    Aug 22 at 13:10






  • 1




    @Servy of course it has to do with threading. I'm not saying multi-threading, I'm saying you have to know if you need to be context aware or not.
    – Crowcoder
    Aug 22 at 13:55

















up vote
7
down vote













Neither of you is correct, see the answer by @erndob for the reasons. However, one of the questions is not answered:




When should we await?




  • Do you want the work to be done sequentially? Use your way.

  • Do you want the work to be done in parallel? Use Jim's way.

Note: Jim's way will not actually run in parallel if the Task Scheduler used is unable to run both Tasks at the same time, for example, due to lack of system resources (thanks @AdamSimon).






share|improve this answer


















  • 1




    Respectfully, the OP states that their understanding is that "the second method (GetAppliancesForHouse) will not wait for the first to finish before starting." This is simply incorrect, and to say that it is not is misleading.
    – wchargin
    Aug 26 at 6:56










  • @wchargin I had originally commented on that but the comment thread was cleaned up. I didn't comment on that on my answer because the OP does not (for me) say whether they are referring to their version (which would be incorrect) or Jim's (which would be correct)
    – Camilo Terevinto
    Aug 26 at 13:35










Your Answer





StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51966518%2fwhen-should-i-await-my-asyncs%23new-answer', 'question_page');

);

Post as a guest






























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
45
down vote



accepted











My understanding is: because the methods in both the furniture and appliance services in the above require House, they will wait for House to be available before continuing.




Your understanding is wrong. The methods that require House, they are not waiting for you to get House because you need it. They don't resolve their dependencies and when to wait for code or not on their own. The code waits to get Houses because you have await before it. It's not aware of what's going to happen next.




Then, both methods that need House will run, but the second method (GetAppliancesForHouse) will not wait for the first to finish before starting.




Similarly, the GetAppliancesForHouse won't have its own understanding if it should wait or not based on the dependencies. GetAppliancesForHouse won't run, because your code says to await GetFurnitureForHouse before it first. Your code will always run sequentially.




Jim's understanding is: that we should await both methods only when they are needed. So that they will both run parallel to each other.




That's generally true. As others have pointed out, the code still might run not in parallel depending on other factors. Also, there might be legitimate reasons to not want to run code in parallel.




He thinks that doing it my way will result in the second method waiting for the first, ie: GetAppliancesForHouse waiting for GetFurnitureForHouse.




He's right.



To see what happens exactly, you can put breakpoints and see what happens after each line.
In Jims case, after going from Furniture to Appliances, furniture variable won't have the value yet, it's still a task, but you are already in the next line.



With your case, going to Appliances line, you will see that Furniture already has the value, since it waited for it.






share|improve this answer


















  • 3




    A demonstration: pastebin.com/ZbLjH6qm
    – GSerg
    Aug 22 at 12:38






  • 4




    @CamiloTerevinto is right. In addition, I'd point out that Jim's way may introduce threading issues. E.g. if EF is used for data access and _applianceService and _furnitureService share the same DbContext instance, there'll be problems as DbContext is not thread-safe.
    – Adam Simon
    Aug 22 at 12:52







  • 5




    @AdamSimon one always has to consider potential threading issues, that's why it is important to understand how async/await works.
    – Crowcoder
    Aug 22 at 13:02










  • @Crowcoder Exactly. But there's so much confusion here about async/await, I considered it important to point out that this aspect must be taken into account, as well.
    – Adam Simon
    Aug 22 at 13:10






  • 1




    @Servy of course it has to do with threading. I'm not saying multi-threading, I'm saying you have to know if you need to be context aware or not.
    – Crowcoder
    Aug 22 at 13:55














up vote
45
down vote



accepted











My understanding is: because the methods in both the furniture and appliance services in the above require House, they will wait for House to be available before continuing.




Your understanding is wrong. The methods that require House, they are not waiting for you to get House because you need it. They don't resolve their dependencies and when to wait for code or not on their own. The code waits to get Houses because you have await before it. It's not aware of what's going to happen next.




Then, both methods that need House will run, but the second method (GetAppliancesForHouse) will not wait for the first to finish before starting.




Similarly, the GetAppliancesForHouse won't have its own understanding if it should wait or not based on the dependencies. GetAppliancesForHouse won't run, because your code says to await GetFurnitureForHouse before it first. Your code will always run sequentially.




Jim's understanding is: that we should await both methods only when they are needed. So that they will both run parallel to each other.




That's generally true. As others have pointed out, the code still might run not in parallel depending on other factors. Also, there might be legitimate reasons to not want to run code in parallel.




He thinks that doing it my way will result in the second method waiting for the first, ie: GetAppliancesForHouse waiting for GetFurnitureForHouse.




He's right.



To see what happens exactly, you can put breakpoints and see what happens after each line.
In Jims case, after going from Furniture to Appliances, furniture variable won't have the value yet, it's still a task, but you are already in the next line.



With your case, going to Appliances line, you will see that Furniture already has the value, since it waited for it.






share|improve this answer


















  • 3




    A demonstration: pastebin.com/ZbLjH6qm
    – GSerg
    Aug 22 at 12:38






  • 4




    @CamiloTerevinto is right. In addition, I'd point out that Jim's way may introduce threading issues. E.g. if EF is used for data access and _applianceService and _furnitureService share the same DbContext instance, there'll be problems as DbContext is not thread-safe.
    – Adam Simon
    Aug 22 at 12:52







  • 5




    @AdamSimon one always has to consider potential threading issues, that's why it is important to understand how async/await works.
    – Crowcoder
    Aug 22 at 13:02










  • @Crowcoder Exactly. But there's so much confusion here about async/await, I considered it important to point out that this aspect must be taken into account, as well.
    – Adam Simon
    Aug 22 at 13:10






  • 1




    @Servy of course it has to do with threading. I'm not saying multi-threading, I'm saying you have to know if you need to be context aware or not.
    – Crowcoder
    Aug 22 at 13:55












up vote
45
down vote



accepted







up vote
45
down vote



accepted







My understanding is: because the methods in both the furniture and appliance services in the above require House, they will wait for House to be available before continuing.




Your understanding is wrong. The methods that require House, they are not waiting for you to get House because you need it. They don't resolve their dependencies and when to wait for code or not on their own. The code waits to get Houses because you have await before it. It's not aware of what's going to happen next.




Then, both methods that need House will run, but the second method (GetAppliancesForHouse) will not wait for the first to finish before starting.




Similarly, the GetAppliancesForHouse won't have its own understanding if it should wait or not based on the dependencies. GetAppliancesForHouse won't run, because your code says to await GetFurnitureForHouse before it first. Your code will always run sequentially.




Jim's understanding is: that we should await both methods only when they are needed. So that they will both run parallel to each other.




That's generally true. As others have pointed out, the code still might run not in parallel depending on other factors. Also, there might be legitimate reasons to not want to run code in parallel.




He thinks that doing it my way will result in the second method waiting for the first, ie: GetAppliancesForHouse waiting for GetFurnitureForHouse.




He's right.



To see what happens exactly, you can put breakpoints and see what happens after each line.
In Jims case, after going from Furniture to Appliances, furniture variable won't have the value yet, it's still a task, but you are already in the next line.



With your case, going to Appliances line, you will see that Furniture already has the value, since it waited for it.






share|improve this answer















My understanding is: because the methods in both the furniture and appliance services in the above require House, they will wait for House to be available before continuing.




Your understanding is wrong. The methods that require House, they are not waiting for you to get House because you need it. They don't resolve their dependencies and when to wait for code or not on their own. The code waits to get Houses because you have await before it. It's not aware of what's going to happen next.




Then, both methods that need House will run, but the second method (GetAppliancesForHouse) will not wait for the first to finish before starting.




Similarly, the GetAppliancesForHouse won't have its own understanding if it should wait or not based on the dependencies. GetAppliancesForHouse won't run, because your code says to await GetFurnitureForHouse before it first. Your code will always run sequentially.




Jim's understanding is: that we should await both methods only when they are needed. So that they will both run parallel to each other.




That's generally true. As others have pointed out, the code still might run not in parallel depending on other factors. Also, there might be legitimate reasons to not want to run code in parallel.




He thinks that doing it my way will result in the second method waiting for the first, ie: GetAppliancesForHouse waiting for GetFurnitureForHouse.




He's right.



To see what happens exactly, you can put breakpoints and see what happens after each line.
In Jims case, after going from Furniture to Appliances, furniture variable won't have the value yet, it's still a task, but you are already in the next line.



With your case, going to Appliances line, you will see that Furniture already has the value, since it waited for it.







share|improve this answer














share|improve this answer



share|improve this answer








edited Aug 27 at 12:15

























answered Aug 22 at 12:17









Erndob

1,196817




1,196817







  • 3




    A demonstration: pastebin.com/ZbLjH6qm
    – GSerg
    Aug 22 at 12:38






  • 4




    @CamiloTerevinto is right. In addition, I'd point out that Jim's way may introduce threading issues. E.g. if EF is used for data access and _applianceService and _furnitureService share the same DbContext instance, there'll be problems as DbContext is not thread-safe.
    – Adam Simon
    Aug 22 at 12:52







  • 5




    @AdamSimon one always has to consider potential threading issues, that's why it is important to understand how async/await works.
    – Crowcoder
    Aug 22 at 13:02










  • @Crowcoder Exactly. But there's so much confusion here about async/await, I considered it important to point out that this aspect must be taken into account, as well.
    – Adam Simon
    Aug 22 at 13:10






  • 1




    @Servy of course it has to do with threading. I'm not saying multi-threading, I'm saying you have to know if you need to be context aware or not.
    – Crowcoder
    Aug 22 at 13:55












  • 3




    A demonstration: pastebin.com/ZbLjH6qm
    – GSerg
    Aug 22 at 12:38






  • 4




    @CamiloTerevinto is right. In addition, I'd point out that Jim's way may introduce threading issues. E.g. if EF is used for data access and _applianceService and _furnitureService share the same DbContext instance, there'll be problems as DbContext is not thread-safe.
    – Adam Simon
    Aug 22 at 12:52







  • 5




    @AdamSimon one always has to consider potential threading issues, that's why it is important to understand how async/await works.
    – Crowcoder
    Aug 22 at 13:02










  • @Crowcoder Exactly. But there's so much confusion here about async/await, I considered it important to point out that this aspect must be taken into account, as well.
    – Adam Simon
    Aug 22 at 13:10






  • 1




    @Servy of course it has to do with threading. I'm not saying multi-threading, I'm saying you have to know if you need to be context aware or not.
    – Crowcoder
    Aug 22 at 13:55







3




3




A demonstration: pastebin.com/ZbLjH6qm
– GSerg
Aug 22 at 12:38




A demonstration: pastebin.com/ZbLjH6qm
– GSerg
Aug 22 at 12:38




4




4




@CamiloTerevinto is right. In addition, I'd point out that Jim's way may introduce threading issues. E.g. if EF is used for data access and _applianceService and _furnitureService share the same DbContext instance, there'll be problems as DbContext is not thread-safe.
– Adam Simon
Aug 22 at 12:52





@CamiloTerevinto is right. In addition, I'd point out that Jim's way may introduce threading issues. E.g. if EF is used for data access and _applianceService and _furnitureService share the same DbContext instance, there'll be problems as DbContext is not thread-safe.
– Adam Simon
Aug 22 at 12:52





5




5




@AdamSimon one always has to consider potential threading issues, that's why it is important to understand how async/await works.
– Crowcoder
Aug 22 at 13:02




@AdamSimon one always has to consider potential threading issues, that's why it is important to understand how async/await works.
– Crowcoder
Aug 22 at 13:02












@Crowcoder Exactly. But there's so much confusion here about async/await, I considered it important to point out that this aspect must be taken into account, as well.
– Adam Simon
Aug 22 at 13:10




@Crowcoder Exactly. But there's so much confusion here about async/await, I considered it important to point out that this aspect must be taken into account, as well.
– Adam Simon
Aug 22 at 13:10




1




1




@Servy of course it has to do with threading. I'm not saying multi-threading, I'm saying you have to know if you need to be context aware or not.
– Crowcoder
Aug 22 at 13:55




@Servy of course it has to do with threading. I'm not saying multi-threading, I'm saying you have to know if you need to be context aware or not.
– Crowcoder
Aug 22 at 13:55












up vote
7
down vote













Neither of you is correct, see the answer by @erndob for the reasons. However, one of the questions is not answered:




When should we await?




  • Do you want the work to be done sequentially? Use your way.

  • Do you want the work to be done in parallel? Use Jim's way.

Note: Jim's way will not actually run in parallel if the Task Scheduler used is unable to run both Tasks at the same time, for example, due to lack of system resources (thanks @AdamSimon).






share|improve this answer


















  • 1




    Respectfully, the OP states that their understanding is that "the second method (GetAppliancesForHouse) will not wait for the first to finish before starting." This is simply incorrect, and to say that it is not is misleading.
    – wchargin
    Aug 26 at 6:56










  • @wchargin I had originally commented on that but the comment thread was cleaned up. I didn't comment on that on my answer because the OP does not (for me) say whether they are referring to their version (which would be incorrect) or Jim's (which would be correct)
    – Camilo Terevinto
    Aug 26 at 13:35














up vote
7
down vote













Neither of you is correct, see the answer by @erndob for the reasons. However, one of the questions is not answered:




When should we await?




  • Do you want the work to be done sequentially? Use your way.

  • Do you want the work to be done in parallel? Use Jim's way.

Note: Jim's way will not actually run in parallel if the Task Scheduler used is unable to run both Tasks at the same time, for example, due to lack of system resources (thanks @AdamSimon).






share|improve this answer


















  • 1




    Respectfully, the OP states that their understanding is that "the second method (GetAppliancesForHouse) will not wait for the first to finish before starting." This is simply incorrect, and to say that it is not is misleading.
    – wchargin
    Aug 26 at 6:56










  • @wchargin I had originally commented on that but the comment thread was cleaned up. I didn't comment on that on my answer because the OP does not (for me) say whether they are referring to their version (which would be incorrect) or Jim's (which would be correct)
    – Camilo Terevinto
    Aug 26 at 13:35












up vote
7
down vote










up vote
7
down vote









Neither of you is correct, see the answer by @erndob for the reasons. However, one of the questions is not answered:




When should we await?




  • Do you want the work to be done sequentially? Use your way.

  • Do you want the work to be done in parallel? Use Jim's way.

Note: Jim's way will not actually run in parallel if the Task Scheduler used is unable to run both Tasks at the same time, for example, due to lack of system resources (thanks @AdamSimon).






share|improve this answer














Neither of you is correct, see the answer by @erndob for the reasons. However, one of the questions is not answered:




When should we await?




  • Do you want the work to be done sequentially? Use your way.

  • Do you want the work to be done in parallel? Use Jim's way.

Note: Jim's way will not actually run in parallel if the Task Scheduler used is unable to run both Tasks at the same time, for example, due to lack of system resources (thanks @AdamSimon).







share|improve this answer














share|improve this answer



share|improve this answer








edited Aug 26 at 13:44

























answered Aug 22 at 12:33









Camilo Terevinto

15.6k63157




15.6k63157







  • 1




    Respectfully, the OP states that their understanding is that "the second method (GetAppliancesForHouse) will not wait for the first to finish before starting." This is simply incorrect, and to say that it is not is misleading.
    – wchargin
    Aug 26 at 6:56










  • @wchargin I had originally commented on that but the comment thread was cleaned up. I didn't comment on that on my answer because the OP does not (for me) say whether they are referring to their version (which would be incorrect) or Jim's (which would be correct)
    – Camilo Terevinto
    Aug 26 at 13:35












  • 1




    Respectfully, the OP states that their understanding is that "the second method (GetAppliancesForHouse) will not wait for the first to finish before starting." This is simply incorrect, and to say that it is not is misleading.
    – wchargin
    Aug 26 at 6:56










  • @wchargin I had originally commented on that but the comment thread was cleaned up. I didn't comment on that on my answer because the OP does not (for me) say whether they are referring to their version (which would be incorrect) or Jim's (which would be correct)
    – Camilo Terevinto
    Aug 26 at 13:35







1




1




Respectfully, the OP states that their understanding is that "the second method (GetAppliancesForHouse) will not wait for the first to finish before starting." This is simply incorrect, and to say that it is not is misleading.
– wchargin
Aug 26 at 6:56




Respectfully, the OP states that their understanding is that "the second method (GetAppliancesForHouse) will not wait for the first to finish before starting." This is simply incorrect, and to say that it is not is misleading.
– wchargin
Aug 26 at 6:56












@wchargin I had originally commented on that but the comment thread was cleaned up. I didn't comment on that on my answer because the OP does not (for me) say whether they are referring to their version (which would be incorrect) or Jim's (which would be correct)
– Camilo Terevinto
Aug 26 at 13:35




@wchargin I had originally commented on that but the comment thread was cleaned up. I didn't comment on that on my answer because the OP does not (for me) say whether they are referring to their version (which would be incorrect) or Jim's (which would be correct)
– Camilo Terevinto
Aug 26 at 13:35

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51966518%2fwhen-should-i-await-my-asyncs%23new-answer', 'question_page');

);

Post as a guest













































































Comments

Popular posts from this blog

Long meetings (6-7 hours a day): Being “babysat” by supervisor

Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

Confectionery