Early Access to Async Action Results in ASP.NET MVC

When we use async action in MVC like below example, post model data is available earlier than 10 seconds in view(here in 1 second) ? I confused, How data available in 1 seccond While fetching data take 10 seconds ???

for example:

    public async Task<ActionResult> GetPosts()
    {

        // ...
        IPost posts = await PostService.GetPosts();// assume this take 10 seconds
        // ...

        return View(model: posts);//BUT we return in 1 second! How posts model available in view for show to the user earlier than 10 seconds?
    }
Jon Skeet
people
quotationmark

You don't get to the return statement in 1 second. The method returns a Task<ActionResult> as soon as it reaches the first await expression which hasn't already completed. That task will not be completed (so you can't get its result) until your async method does reach the return statement.

But when the first thing you're awaiting completes, your async method will resume, continuing from where it left off until the next await, when it will "go to sleep" again (but without blocking a thread) until the awaitable has completed, etc.

That's the nature of async. You just need to distinguish between "the async method has returned a task" (which happens quickly) and "the async method has completed" (which often doesn't happen quickly).

It's hard to describe async thoroughly in just a few paragraphs - I suggest you read a good book, watch a video, or read a good tutorial. MSDN is a good starting point.

people

See more on this question at Stackoverflow