-1

I was going through some code to learn how to Consume a web API.

public async Task<List<TodoItem>> RefreshDataAsync ()
{

  // RestUrl = http://developer.xamarin.com:8081/api/todoitems/
  var uri = new Uri (string.Format (Constants.RestUrl, string.Empty));
  var response = await client.GetAsync (uri);
  if (response.IsSuccessStatusCode) {
      var content = await response.Content.ReadAsStringAsync ();
      Items = JsonConvert.DeserializeObject <List<TodoItem>> (content);
  }
}

I found this code.All i want to know is why we use Async and Await. As i observed Await is mandatory in method body when function is encapsulated as Async keyword.

ammad khan
  • 1,022
  • 1
  • 10
  • 14
  • 1
    This is not web api specific but more a question on why you should use async-await in asp.net. Try to elaborate the title of this question – Marcus Höglund Jan 29 '18 at 12:38

1 Answers1

0

It depends on your application. For GUI apps, it keeps the user interface from freezing and allows this thread to do other things while the call is being made. For apis, it can allow for scalability if you have blocking IO operations. Here is a similar question with some great explanations: Why should I create async WebAPI operations instead of sync ones?

Kyle Dodge
  • 834
  • 7
  • 17