Talk:Async/await

Latest comment: 2 years ago by 2603:6010:8202:5901:3F0F:D4BD:99CB:5C67 in topic Language and Compiler Support

Untitled edit

The article doesn't describe the flow of control executed by an await command. It only describes what it doesn't do, then goes right into implementation details. I've read the article twice and still have no idea what "await" does. — Preceding unsigned comment added by 2602:304:B390:1700:B159:7D37:18B8:9E75 (talk) 15:53, 21 March 2017 (UTC)Reply

+1. I saw the same problem; the article gives examples and implementation, but not an explanation of what the feature actually does; what all the examples have in common. — Preceding unsigned comment added by CTMacUser (talkcontribs) 16:34, 18 August 2017 (UTC)Reply

I went ahead and added a rough draft explaining the async/await feature. I haven't cited any sources yet (though most of the information can be sourced by C#'s official documentation on the subject as well as Eric Lippert's .NET blogs) and haven't honed down appropriate pages for the dead links, but it's a start. Would you say it helps clarify the issue more clearly than the previous version of the article? Compynerd255 (talk) 23:51, 23 July 2018 (UTC)Reply

"Benefits and Criticisms" section edit

I added a "Benefits and Criticisms" section in order to provide room for quotes from well-renowned CS experts on the async/await pattern. Currently, the article reads with just a factual description of the subject that leans a little too much on its promised benefits, but I do remember reading criticisms of this pattern when it was newer, such as a complaint that it overtakes your program like a "zombie virus". I don't have time to research this myself, so could someone else add a few of these? Compynerd255 (talk) 15:53, 24 July 2018 (UTC)Reply

Possible added sections edit

I wonder if the page would be improved by the following explanation of the syntax from a fairly language-neutral perspective. LeoNerd (talk) 16:05, 20 August 2019 (UTC)Reply

Syntax edit

The async/await pattern is usually expressed in the form of two additional keywords, named async and await, which interact with values of a type used to represent a deferred operation, often called a promise or future. While use of these two terms often varies from language to language, the word promise will be used in the following description.

The await keyword operates on an expression that provides a promise and pauses execution of the containing function if the result of that promise is not yet available. The function remains suspended until the promise is fulfilled, at which point the function will resume execution. The await expression itself will yield the result of the promise.

The async keyword annotates the definition of a function and has two effects. It permits the body of the function to use an await expression, and it alters the return type or value of the function, so that it is wrapped in a promise.

Because an async-annotated function always returns its result via a promise, it is permitted to use an await expression within it. The first time the function is required to suspend because of an await expression on an unresolved promise, it can return an unresolved promise of its own to its caller thus providing a mechanism to eventually yield its result when it resumes again.

Advantages edit

The async/await syntax can be seen as a development from earlier techniques, such as the .then() method in JavaScript and C#. While using a .then() method it is possible to chain a sequence of operations together one after another, more difficulty comes in trying to express patterns like repeating loops, iteration over a list, or perform nested conditional operations. Because the body of each .then() invocation has to be a self-contained function in its own right, syntactical structures like loops or conditionals cannot span multiple promise-based operations.

Conversely, because each await expression operates on a promise but sits inside a function, the operation of the function itself can proceed by waiting on each promise in turn. Code structures like loops can contain await expressions inside them, allowing the loop to suspend and resume later on. This leads to a much more natural way to write promise-driven code, because the programmer can use the familiar language features of control flow even when dealing with promise-based asynchronous behaviour.

Invention and adoption edit

Who invented it and when? What is the timeline of adoption in the various languages? Encyclopedant (talk) 17:27, 30 October 2019 (UTC)Reply

This would be good to have in the article. BernardoSulzbach (talk) 17:41, 31 October 2019 (UTC)Reply

Language and Compiler Support edit

More and more languages have adopted coroutine, so do compiler implementations. In section 8, "In C++", I believe the statement "GCC still has no support for it" need update. See

 * gcc doc
 * Summaries of current compiler support 
 * Compilable and executable demo  — Preceding unsigned comment added by 2603:6010:8202:5901:3F0F:D4BD:99CB:5C67 (talk) 16:29, 23 June 2021 (UTC)Reply