0

I created a new ASP.Net 5 application and in the generated code there is much use of await keyword, for instance:

var result = await _userManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
    await _signInManager.SignInAsync(user, isPersistent: false);
    return RedirectToAction(nameof(HomeController.Index), "Home");
}

My question is what do we accomplish by this? We still need to wait for result to be fetched, we can't proceed further until we don't get the result. So what is the point of await here?

Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
Aleksa
  • 2,976
  • 4
  • 30
  • 49
  • You might ask, what is the point of `await` *at any time* using the same reasoning. This code is not special in any way. – usr Nov 25 '15 at 16:17
  • 4
    Probably the biggest thing is that you *could* be proceeding with other work. And, in the context of ASP, if you don't have anything else to do, the executing thread can drop back to the threadpool and service other requests until your result is ready. – Glorin Oakenfoot Nov 25 '15 at 16:17

2 Answers2

1

The advantage is that the control is returned to the caller of the current method. Your async method will "resume" when the task awaited is completed.

This is very useful because it removes the need to manage threads and/or callbacks.

Edit:

Here's why you would want (or not) to use async/await. Let's say you have a network call like this:

public RequestResult Request(string queryString)
{
    var response = connection.Request(queryString);
}

This is a blocking call, it'll stop the current executing thread and wait until the call is completed. There could be an Async version of the Request(string) method, usually suffixed with Async. If so, you can await it in an async Task method.

public RequestResult Request(string queryString)
{
    var response = connection.Request(queryString);
}

The control will be released until the task is completed, allowing the caller to continue. This is very useful for a GUI application. You can bind an async method to an event, have it fired, start the request and keep the UI updating while the request completes.

Before async/await, the prefered method was using Begin/End methods. You would pass a delegate to the Begin method and it would get called when the task was done. I want to point out, this can still be used! Here are more details on async patterns. async/await is called TAP, Task based async programming.

Edit: The C# compiler will take the rest of the method after an await keyword and make it sort of a callback (internally).

Philippe Paré
  • 4,279
  • 5
  • 36
  • 56
  • 1
    Well, without await there would not be any thread management or callbacks either. The code would be even simpler. – usr Nov 25 '15 at 16:18
  • Yes, there is in fact no point in using async/await if you are just calling `MyTask().Wait()`, but without any other context, we can't tell if there is a need to use async/await – Philippe Paré Nov 25 '15 at 16:19
  • I'll add details to, hopefully, help you grasp the use of `async/await`. – Philippe Paré Nov 25 '15 at 16:30
  • Thanks for explanation. I understand how it works and why it is useful in long IO operations, but what about long CPU bound operations? If I understood correctly they still require a new thread and async/await shouldn't be used for them, right? – Aleksa Nov 26 '15 at 09:56
  • The task api will create a task if it needs to. It has a certain amount of already created threads, saving thr creation time of that thread, sometime crutial. And you still get the code-efficience of async/await – Philippe Paré Nov 26 '15 at 12:20
0

Presumably, that all happens inside a function with the async keyword. Consider the caller of that function - they're also awaiting something (this function!) and they might very well be able to proceed doing other things in the meantime.

In the context of ASP.NET, some ways down the stack the application is catching incoming requests and calling methods on your controllers to calculate what to send as a response. If your controller actions are async, your application can multi-task (pun intented...) and serve more request faster, without having to wait for e.g. database or I/O.

Tomas Aschan
  • 58,548
  • 56
  • 243
  • 402