Understanding async and await

纵饮孤独 提交于 2019-12-01 07:41:19

问题


I am trying to learn the details of MVC5 and I came across something that baffles me. In the accountController that comes by default with an MVC5 project with Individual Auth there is this line of code in the async Login and Register methods

var result = await UserManager.CreateAsync(user, model.Password);

I read here http://msdn.microsoft.com/en-us/library/hh191443.aspx that this is a normal practice, but I do not understand why you would ever use an asynchronous method and await in the same line. Wouldn't it make more sense to just use the .Create method that takes the same parameters here?


回答1:


The difference between SomeMethod() and await SomeMethodAsync() is that the latter won't block a thread while the method performs IO. Because of that, the application becomes more scalable, because it can use smaller number of threads to serve the same number of requests.

If you don't care about scalability, then it doesn't matter much which of the two options are you going to choose. But it's probably still better to use the async version, to future-proof your application, so that your application behaves well when scalability becomes an issue.



来源:https://stackoverflow.com/questions/20715175/understanding-async-and-await

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!