词条 | Async/await |
释义 |
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. It is semantically related to the concept of a coroutine and is often implemented using similar techniques, but is primarily intended to provide opportunities for the program to execute other code while waiting for a long-running, asynchronous task to complete, usually represented by promises or similar data structures. The feature is found in C# 5.0, Python 3.5, Hack, Dart, Kotlin 1.1, and JavaScript, with some experimental work in extensions, beta versions, and particular implementations of Scala[1], Rust[2], and C++. Example C#The C# function below, which downloads a resource from a URI and returns the resource's length, uses this async/await pattern:
A function using async/await can use as many In the particular case of C#, and in many other languages with this language feature, the async/await pattern is not a core part of the language's runtime, but is instead implemented with lambdas or continuations at compile time. For instance, the C# compiler would likely translate the above code to something like the following before translating it to its IL bytecode format: Because of this, if an interface method needs to return a promise object, but itself does not require One important caveat of this functionality, however, is that while the code resembles traditional blocking code, the code is actually non-blocking and potentially multithreaded, meaning that many intervening events may occur while waiting for the promise targeted by an In F#An F# release of 2007 featured asynchronous workflows.[3]. In this initial version, In C#In C# versions before C# 7, async methods are required to return either Methods that make use of The following async method downloads data from a URL using In ScalaIn the experimental Scala-async extension to Scala, {{Java|await}} is a "method", although it does not operate like an ordinary method. Furthermore, unlike in C# 5.0 in which a method must be marked as async, in Scala-async, a block of code is surrounded by an async "call". How it worksIn Scala-async, {{Java|async}} is actually implemented using a Scala macro, which causes the compiler to emit different code, and produce a finite state machine implementation (which is considered to be more efficient than a monadic implementation, but less convenient to write by hand). There are plans for Scala-async to support a variety of different implementations, including non-asynchronous ones. In PythonPython 3.5 has added support for Async/Await as described in PEP0492 (https://www.python.org/dev/peps/pep-0492/). In JavaScriptThe await operator in JavaScript can only be used from inside an async function. If the parameter is a promise, execution of the async function will resume when the promise is resolved (unless the promise is rejected, in which case an error will be thrown that can be handled with normal JavaScript exception handling.) If the parameter is not a promise, the parameter itself will be returned immediately.[5] Many libraries provide promise objects that can also be used with await, as long as they match the specification for native JavaScript promises. However, promises from the jQuery library were not Promises/A+ compatible until jQuery 3.0.[6] Here's an example (modified from this[7] article): Node.js version 8 includes a utility that enables using the standard library callback-based methods as promises.[8]In C++In C++, await (named co_await in C++) has been officially merged into C++20 draft, so it is on the course to be formally accepted as a part of official C++20 [9]; also MSVC and Clang compilers are already supporting at least some form of co_await (GCC still has no support for it). In Perl5The [https://metacpan.org/pod/Future::AsyncAwait Future::AsyncAwait] module was the subject of a Perl foundation grant in September 2018[10]. Benefits and criticismsA 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. In particular, it has been argued that await is the best way of writing asynchronous code in message-passing programs; in particular, being close to blocking code, readability and the minimal amount of boilerplate code were cited as await benefits.[11] As a result, async/await makes it easier for most programmers to reason about their programs, and await tends to promote better, more robust non-blocking code in applications that require it. Such applications range from programs presenting graphical user interfaces to massively scalable stateful server-side programs, such as games and financial applications. When criticising await, it has been noted that await tends to cause surrounding code to be asynchronous too; on the other hand, it has been argued that this contagious nature of the code (sometimes being compared to a "zombie virus") is inherent to all kinds of asynchronous programming, so await as such is not unique in this regard.[4] See also
References1. ^{{cite web|url=https://github.com/scala/async|title=Scala Async|accessdate=20 October 2013}} 2. ^{{Cite web|url=https://github.com/alexcrichton/futures-await|title=alexcrichton/futures-await|website=GitHub|language=en|access-date=2018-03-29}} 3. ^{{cite web|url=https://blogs.msdn.microsoft.com/dsyme/2007/10/10/introducing-f-asynchronous-workflows/|title=Introducing F# Asynchronous Workflows|publisher=}} 4. ^1 Stephen Cleary, [https://msdn.microsoft.com/en-us/magazine/jj991977.aspx Async/Await - Best Practices in Asynchronous Programming] 5. ^{{cite web|url=https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await|title=await - JavaScript (MDN)|accessdate=2 May 2017}} 6. ^{{cite web|url=https://jquery.com/upgrade-guide/3.0/#breaking-change-and-feature-jquery-deferred-is-now-promises-a-compatible|title=jQuery Core 3.0 Upgrade Guide|accessdate=2 May 2017}} 7. ^{{cite web|url=http://pouchdb.com/2015/03/05/taming-the-async-beast-with-es7.html|title=Taming the asynchronous beast with ES7|accessdate=12 November 2015}} 8. ^{{cite web|url=https://nodejs.org/en/blog/release/v8.0.0/#improved-support-for-promises|title=Node v8.0.0 (Current) - Node.js|first=Node.js|last=Foundation|website=Node.js}} 9. ^{{cite web|url=https://hub.packtpub.com/iso-c-committee-announces-that-c20-design-is-now-feature-complete/|title=ISO C++ Committee announces that C++20 design is now feature complete}} 10. ^{{Cite web|url=http://news.perlfoundation.org/2018/11/september-2018-grant-votes.html|title=September 2018 Grant Votes - The Perl Foundation|website=news.perlfoundation.org|access-date=2019-03-26}} 11. ^'No Bugs' Hare. Eight ways to handle non-blocking returns in message-passing programs CPPCON, 2018 [https://fullstackgeek.blogspot.com/2019/02/asynchronous-javascript-explained-for-beginners.html FullStackgGeek] 1 : Control flow |
随便看 |
|
开放百科全书收录14589846条英语、德语、日语等多语种百科知识,基本涵盖了大多数领域的百科知识,是一部内容自由、开放的电子版国际百科全书。