Theasync function declaration creates a binding of a new async function to a given name. The await keyword is permitted within the function body, enabling asynchronous, promise-based behavior to be written in a cleaner style and avoiding the need to explicitly configure promise chains.
Note: There cannot be a line terminator between async and function, otherwise a semicolon is automatically inserted, causing async to become an identifier and the rest to become a function declaration.
An async function declaration creates an AsyncFunction object. Each time when an async function is called, it returns a new Promise which will be resolved with the value returned by the async function, or rejected with an exception uncaught within the async function.
Async functions can contain zero or more await expressions. Await expressions make promise-returning functions behave as though they're synchronous by suspending execution until the returned promise is fulfilled or rejected. The resolved value of the promise is treated as the return value of the await expression. Use of async and await enables the use of ordinary try / catch blocks around asynchronous code.
Note that even though the return value of an async function behaves as if it's wrapped in a Promise.resolve, they are not equivalent. An async function will return a different reference, whereas Promise.resolve returns the same reference if the given value is a promise. It can be a problem when you want to check the equality of a promise and a return value of an async function.
The body of an async function can be thought of as being split by zero or more await expressions. Top-level code, up to and including the first await expression (if there is one), is run synchronously. In this way, an async function without an await expression will run synchronously. If there is an await expression inside the function body, however, the async function will always complete asynchronously.
Code after each await expression can be thought of as existing in a .then callback. In this way a promise chain is progressively constructed with each reentrant step through the function. The return value forms the final link in the chain.
Note how the promise chain is not built-up in one go. Instead, the promise chain is constructed in stages as control is successively yielded from and returned to the async function. As a result, we must be mindful of error handling behavior when dealing with concurrent asynchronous operations.
For example, in the following code an unhandled promise rejection error will be thrown, even if a .catch handler has been configured further along the promise chain. This is because p2 will not be "wired into" the promise chain until control returns from p1.
In sequentialStart, execution suspends 2 seconds for the first await, and then another second for the second await. The second timer is not created until the first has already fired, so the code finishes after 3 seconds.
In sequentialWait, both timers are created and then awaited. The timers run concurrently, which means the code finishes in 2 rather than 3 seconds, i.e. the slowest timer. However, the await calls still run in series, which means the second await will wait for the first one to finish. In this case, the result of the fastest timer is processed after the slowest.
In sequentialWait, if promise fast rejects before promise slow is fulfilled, then an unhandled promise rejection error will be raised, regardless of whether the caller has configured a catch clause.
In concurrent1, Promise.all wires up the promise chain in one go, meaning that the operation will fail-fast regardless of the order of rejection of the promises, and the error will always occur within the configured promise chain, enabling it to be caught in the normal way.
In the two rewritten versions, notice there is no await statement after the return keyword, although that would be valid too: The return value of an async function is implicitly wrapped in Promise.resolve - if it's not already a promise itself (as in the examples).
\n Note: The purpose of async/await is to simplify the syntax\n necessary to consume promise-based APIs. The behavior\n of async/await is similar to combining generators and\n promises.\n
\n The body of an async function can be thought of as being split by zero or more await\n expressions. Top-level code, up to and including the first await expression (if there is\n one), is run synchronously. In this way, an async function without an await expression\n will run synchronously. If there is an await expression inside the function body,\n however, the async function will always complete asynchronously.\n
\n Code after each await expression can be thought of as existing in a .then\n callback. In this way a promise chain is progressively constructed with each reentrant\n step through the function. The return value forms the final link in the chain.\n
\n Note how the promise chain is not built-up in one go. Instead, the promise chain is\n constructed in stages as control is successively yielded from and returned to the async\n function. As a result, we must be mindful of error handling behavior when dealing with\n concurrent asynchronous operations.\n
\n For example, in the following code an unhandled promise rejection error will be thrown,\n even if a .catch handler has been configured further along the promise\n chain. This is because p2 will not be \"wired into\" the promise chain until\n control returns from p1.\n
\n In sequentialStart, execution suspends 2 seconds for the first\n await, and then another second for the second await. The\n second timer is not created until the first has already fired, so the code finishes\n after 3 seconds.\n
\n In sequentialWait, both timers are created and then awaited.\n The timers run concurrently, which means the code finishes in 2 rather than 3 seconds,\n i.e. the slowest timer.\n However, the await calls still run in series, which means the second\n await will wait for the first one to finish. In this case, the result of\n the fastest timer is processed after the slowest.\n
\n In sequentialWait, if promise fast rejects before promise\n slow is fulfilled, then an unhandled promise rejection error will be\n raised, regardless of whether the caller has configured a catch clause.\n
\n In concurrent1, Promise.all wires up the promise\n chain in one go, meaning that the operation will fail-fast regardless of the order of\n rejection of the promises, and the error will always occur within the configured\n promise chain, enabling it to be caught in the normal way.\n
\n In the two rewritten versions, notice there is no await statement after the\n return keyword, although that would be valid too: The return value of an\n async function is implicitly wrapped in Promise.resolve - if\n it's not already a promise itself (as in the examples).\n
There is quite a bit of debate about async vs threads, function coloring in async, etc. I don't recommend going by just one blog article. It is better to learn it yourself and then reexamine the issues and tradeoffs.
If you have decided to use Rust async, it is worth asking why. There are performance benefits, but as the blog article and the Rust async book itself says, not everyone needs this level of performance. The async book has information about async pros and cons, and it seems fairly balanced to me. So I would start there.
But another reason to learn async, or at least become familiar with it, may be to use tools in the Rust ecosystem that themselves use async, such as a web server or web framework. In that case the blog article and similar debates are not really applicable.
But it looks like the author is mainly addressing themselves with the title, not recommending everyone else to avoid async Rust. And keep in mind people who are content with async are not usually making blogs about it.
Expanding on what @jumpnbrownweasel said, if you're going to do web development, pretty much every Rust framework for it is async, also sqlx the most popular rust database library is async, so in cases like these if you really want to avoid async you'll either have to find some more obscure alternative or write it yourself.
I read the article last night and my takeaways are that the author doesn't have a need for async. This is fine, and very much expected. Most users of async don't need it [1]. And my reading is that the author argues passionately that others don't need it, either. They may be right. Async is grossly overused where it adds complexity that doesn't hold up its own weight.
3a8082e126