Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use the "await" keyword

Tags:

I'm writing a web page, and it calls some web services. The calls looked like this:

var Data1 = await WebService1.Call(); var Data2 = await WebService2.Call(); var Data3 = await WebService3.Call(); 

During code review, somebody said that I should change it to:

var Task1 = WebService1.Call(); var Task2 = WebService2.Call(); var Task3 = WebService3.Call();  var Data1 = await Task1; var Data2 = await Task2; var Data3 = await Task3; 

Why? What's the difference?

like image 360
Eric B Avatar asked May 30 '13 19:05

Eric B


People also ask

When should await be used?

The await operator is used to wait for a Promise . It can only be used inside an async function within regular JavaScript code; however it can be used on its own with JavaScript modules.

How do you use await?

Inside an async function, you can use the await keyword before a call to a function that returns a promise. This makes the code wait at that point until the promise is settled, at which point the fulfilled value of the promise is treated as a return value, or the rejected value is thrown.

What is the use of await keyword in C#?

The await operator suspends evaluation of the enclosing async method until the asynchronous operation represented by its operand completes. When the asynchronous operation completes, the await operator returns the result of the operation, if any.

Should I use await or result?

Answers. Always follow the standard async/await programming pattern from top down. Do not use . Result() as it is only used to invoke an async method from synchronous code.


1 Answers

Servy's answer is correct; to expand on that a little. What's the difference between:

Eat(await cook.MakeSaladAsync()); Eat(await cook.MakeSoupAsync()); Eat(await cook.MakeSandwichAsync()); 

and

Task<Salad> t1 = cook.MakeSaladAsync(); Task<Soup> t2 = cook.MakeSoupAsync(); Task<Sandwich> t3 = cook.MakeSandwichAsync(); Eat(await t1); Eat(await t2); Eat(await t3); 

?

The first is:

  • Cook, please make me a salad
  • While waiting for the salad, you have some free time to brush the cat. When you're done that, oh, look, the salad is done. If the cook finished the salad while you were brushing the cat, they did not start on making the soup because you haven't asked for it yet.
  • Eat the salad. The cook is now idle while you eat.
  • Cook, please make me some soup.
  • While waiting for the soup you have some free time to clean the fish tank. When you're done that, oh, look, the soup is done. If the cook finishes the soup while you are cleaning the fish tank, they do not start on the sandwich because you haven't asked for it yet.
  • Eat the soup. The cook is now idle while you eat.
  • Cook, please make me a sandwich.
  • Again, find something else to do while you're waiting.
  • Eat the sandwich.

Your second program is equivalent to:

  • Cook, please make me a salad
  • Cook, please make me some soup.
  • Cook, please make me a sandwich.
  • Is the salad done? If not, while waiting for the salad, you have some free time to brush the cat. If the cook finished the salad while you were brushing the cat, they started making the soup.
  • Eat the salad. The cook can still be working on the soup and sandwich while you are eating.
  • Is the soup done? ...

You see the difference? In your original program you don't tell the cook to start the next course until you are done eating the first course. In your second program you request all three courses up front, and eat them -- in order -- as they come available. The second program makes better use of the cook's time because the cook can "get ahead" of you.

like image 132
Eric Lippert Avatar answered Sep 21 '22 17:09

Eric Lippert