Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

async/await - Is this understanding correct?

After two questions and much confusion - I wonder if I finally got it right. This is my understanding:

async/await serves only one purpose - to allow code to be executed after an already asynchronous task is finished. e.g.

async Task CallerMethod()
{
     await AsyncMethod();
     AnotherMethod();
}

allows AnotherMethod to be executed after the asynchronous AsyncMethod is finished instead of immediately after AsyncMethod is started.

async/await NEVER makes anything asynchronous. It doesn't start a separate thread (unless the awaited method does that anyway, of course), etc.

Is my understanding (finally) correct?

like image 252
ispiro Avatar asked Oct 11 '15 17:10

ispiro


People also ask

What is the meaning of async await?

"async and await make promises easier to write" async makes a function return a Promise. await makes a function wait for a Promise.

Should I 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.

Why async await is used instead of promises?

Async/Await is used to work with promises in asynchronous functions. It is basically syntactic sugar for promises. It is just a wrapper to restyle code and make promises easier to read and use. It makes asynchronous code look more like synchronous/procedural code, which is easier to understand.

Is async await really asynchronous?

In computer programming, the async/await pattern is a syntactic feature of many programming languages that allows an asynchronous, non-blocking function to be structured in a way similar to an ordinary synchronous function.


2 Answers

Though Stephen's answer is correct, I want to make sure that you've got a few additional points clear.

async/await NEVER makes anything asynchronous

It makes CallerMethod an asynchronous method. CallerMethod returns a Task which can itself be awaited, and CallerMethod returns that task before the work of CallerMethod is itself complete, so it is an asynchronous method.

It doesn't make AsyncMethod asynchronous; it was already asynchronous.

It doesn't start a separate thread

Right. This is a frequent source of confusion. Threads are a unit of concurrency, which is only one kind of asynchrony. An analogy usually helps. You can put the bread in the toaster, wait for it to get toasted, and then make the eggs. You can put the bread in the toaster, cook the eggs while the toast is toasting, and then deal with the toast after the eggs are done. Or you can hire two cooks, one to cook eggs and one to make toast. The first is synchronous processing. The second and third are asynchronous, but only the third is concurrent. Notice that the third solution is the most expensive; threads are workers and workers aren't cheap.

What makes an asynchronous method asynchronous is not that it is concurrent -- though it might be. What makes it asynchronous is that it gives you a mechanism that allows you to do something else while you're waiting for its work to complete. await is just a pleasant way to write "here's the code I want you to run after the task has completed successfully".

like image 59
Eric Lippert Avatar answered Oct 28 '22 05:10

Eric Lippert


async/await NEVER makes anything asynchronous. It doesn't start a separate thread (unless the awaited method does that anyway, of course), etc.

Yes.

Put another way, async/await enables writing code from an asynchronous perspective. That is, code can "asynchronously wait" (await) operations to complete, and in turn define its own asynchronous operation (the task returned from an async method, which represents the execution of that method).

like image 38
Stephen Cleary Avatar answered Oct 28 '22 05:10

Stephen Cleary