Async and Await in ApiController Post

前端 未结 2 1095
孤独总比滥情好
孤独总比滥情好 2021-02-05 14:07

I\'m still not quite clear about async and await in .net 4.5. So far, I think I understand that await:

  1. puts the function (to its right) on a separate thread.
2条回答
  •  灰色年华
    2021-02-05 14:35

    puts the function (to its right) on a separate thread.

    No. async does not start a new thread. I have an async intro that you may find helpful.

    Post will finish execution and return control to whoever called myApiController.Post(obj). But I don't have the HttpResponseMessage object yet

    Correct.

    In this above simple example, would the call immediately return to the client (that is, client JS website or mobile app)?

    No. ASP.NET MVC 4.5 sees that you're returning Task and not HttpResponseMessage, so it will not send the response until your Task is completed (at the end of your async Post method).

提交回复
热议问题