Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are async/await usefull for web applications? [closed]

I read about Async and Await Keywords in C# 5.0, which allows parallel tasks to be executed at the same time. (this is what i understood).

Is this practice useful for web applications?

Here, we have AJAX methods which allows tasks to be executed without blocking the UI (the browser).

Should they be used only for asynchronous web services?

Can you give me an example of why/howto use async/await keywords in a web applications? (to take advantage of this features)

like image 371
Catalin Avatar asked Jan 29 '13 07:01

Catalin


People also ask

Should I always use async await?

Async/Await makes it easier to write promises. The keyword 'async' before a function makes the function return a promise, always. And the keyword await is used inside async functions, which makes the program wait until the Promise resolves.

What happens if we dont use async await?

The call to the async method starts an asynchronous task. However, because no Await operator is applied, the program continues without waiting for the task to complete. In most cases, that behavior isn't expected.

Does async await work in browser?

As you can see, the async and await keywords are supported by a majority of up-to-date browsers.

What is one benefit of using async await?

A significant benefit of the async/await pattern in languages that support it is that asynchronous, non-blocking code can be written, with minimal overhead, and looking almost like traditional synchronous, blocking code.


2 Answers

Perhaps the best example i can think of is calling a series of web service methods from your Asynchronous Action method. For example:

Lets say you have two methods defined in your web service, Method1 and Method2. If each method takes 1 second, calling both of them synchronously would take 2 seconds, however calling them in a async fashion would take less time.

 using (DataClient proxy = new DataClient())
            {
                Task<Object> data1= proxy.Method1();
                Task<Object> data2 = proxy.Method2();

                await Task.WhenAll(new Task[] { data1, data2 });

               //Completed
           }

From MSDN:

The AsyncController class enables you to write asynchronous action methods. You can use asynchronous action methods for long-running, non-CPU bound requests. This avoids blocking the Web server from performing work while the request is being processed. A typical use for the AsyncController class is long-running Web service calls.

Link: http://msdn.microsoft.com/en-us/library/ee728598(v=VS.98).aspx

like image 108
TT77 Avatar answered Oct 06 '22 17:10

TT77


async and await it's not an innovation, i't simple syntax sugar arround the same async invokation handling pattern, with the difference that it makes writing code in linear, much more natural way for a coder.

So, the real question is: where should I use async pattern ?

Well, whenever you need some async invokation, or massive proecssing of multiple calls, or some heavy processing operations... or whatever else can be found in universe to fit async needs.

like image 45
Tigran Avatar answered Oct 06 '22 18:10

Tigran