Issue
We use the term "Async" to allow the code stream to continue without blocking the main stream. This is OK. We want the mainstream to continue without waiting for that process to complete. But usually, "Async and Await" are used together.
My question is; When we add "await", we expect "Async" status in the code stream. In this case, I do not understand what is the benefit of using "Async"? Can someone explain this, thank you, good work.
Solution
It's not clear which technology you're referring to. I'll assume you're referring to the async
and await
keywords that have been added to many languages the last few years.
In that case, this statement is actually incorrect:
We use the term "Async" to allow the code stream to continue without blocking the main stream.
In C#, JavaScript, Python, F#, and Visual Basic, the async
keyword does not act asynchronously. It has two effects:
- Enable the
await
keyword within that method/lambda. - Transform that method/lambda into a state machine.
So async
does not mean "run this code on a background thread" or anything like that, which is a common but incorrect assumption.
The first point above is important for backwards compatibility. Each of these languages use await
as a contextual keyword, so the text "await" in a program is only a keyword if it is in a method marked with async
. This allows the languages to introduce the new keywords without breaking any existing code (e.g., someone's code that used await
as a variable name).
See this post of mine which collected a lot of the discussion around the keywords as they were originally designed for C#/VB. Those same design decisions carried over almost exactly to other languages as they adopted the keywords, too. One of the resources linked from that post is Eric Lippert's post on why we need an async
keyword in addition to an await
keyword.
Answered By - Stephen Cleary
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.