Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why was await* removed from the async/await proposal?

The only place it seems to be documented is this issue thread and the actual specification. However, the reasoning for the removal isn't posted anywhere I could find.

The new recommended way seems to be await Promise.all(), but I'm curious as to why await* was removed.

like image 922
m0meni Avatar asked Feb 03 '16 03:02

m0meni


People also ask

Why is await used with async?

Use of async and await enables the use of ordinary try / catch blocks around asynchronous code. Note: The await keyword is only valid inside async functions within regular JavaScript code. If you use it outside of an async function's body, you will get a SyntaxError .

Is await async blocking?

The await operator doesn't block the thread that evaluates the async method. When the await operator suspends the enclosing async method, the control returns to the caller of the method.

Does async need Athen or await?

We recommend using async/await where possible, and minimize promise chaining. Async/await makes JavaScript code more accessible to developers that aren't as familiar with JavaScript, and much easier to read.

What happens to await if promise is rejected?

If the promise is rejected, the await expression throws the rejected value. The function containing the await expression will appear in the stack trace of the error. Otherwise, if the rejected promise is not awaited or is immediately returned, the caller function will not appear in the stack trace.


1 Answers

Well, the last revision of the readme before it was removed already mentions everything in that paragraph:

await* and parallelism

In generators, both yield and yield* can be used. In async functions, only await is allowed. The direct analogoue of yield* does not make sense in async functions because it would need to repeatedly await the inner operation, but does not know what value to pass into each await (for yield*, it just passes in undefined because iterators do not accept incoming values).

It has been suggested that the syntax could be reused for different semantics - sugar for Promise.all. This would accept a value that is an array of Promises, and would (asynchronously) return an array of values returned by the promises. This is expected to be one of the most common Promise-related oprerations that would not yet have syntax sugar after the core of this proposal is available.

So it's no direct analogue to yield* as one might expect, it doesn't really make sense, it was only a suggestion but never really included in the spec proposal.

The consensus was that there is no reason to introduce more syntactical sugar than necessary, calling Promise.all is not much of a difference.

You can check out the discussions in issue 8 or issue 29.

Finally, proposals for mightier weapons (parallelism) are still under way. Check out async iteration, async generators and observables. There might be some that could use an await* keyword much better than for simple arrays of promises.

The async/await proposal is minimal and only introduces the necessary primitives. There is no bikeshedding about possible extensions, which should be discussed separately.

like image 111
Bergi Avatar answered Oct 27 '22 00:10

Bergi