When should I await my asyncs?
Clash Royale CLAN TAG#URR8PPP
up vote
40
down vote
favorite
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?
c# .net async-await
 |Â
show 7 more comments
up vote
40
down vote
favorite
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?
c# .net async-await
Could you show us the source code forGetFurnitureForHouse
?
â 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
 |Â
show 7 more comments
up vote
40
down vote
favorite
up vote
40
down vote
favorite
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?
c# .net async-await
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?
c# .net async-await
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 forGetFurnitureForHouse
?
â 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
 |Â
show 7 more comments
Could you show us the source code forGetFurnitureForHouse
?
â 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
 |Â
show 7 more comments
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.
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
 |Â
show 6 more comments
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).
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
add a comment |Â
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.
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
 |Â
show 6 more comments
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.
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
 |Â
show 6 more comments
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.
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.
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
 |Â
show 6 more comments
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
 |Â
show 6 more comments
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).
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
add a comment |Â
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).
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
add a comment |Â
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).
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).
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
add a comment |Â
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
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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