Skip to content
This repository has been archived by the owner on Jan 26, 2022. It is now read-only.

Use async for instead of for await #24

Closed
alexeyraspopov opened this issue Apr 3, 2016 · 8 comments
Closed

Use async for instead of for await #24

alexeyraspopov opened this issue Apr 3, 2016 · 8 comments

Comments

@alexeyraspopov
Copy link

Python's PEP-0492 (Coroutines with async and await syntax) has good explanation of why it should be

async for (let item of items) {
  // ...
}

instead of

for await (let item of items) {
  // ...
}

await means "wait next expression to be resolved" that conflicts with meaning of cycles. async means "next block has asynchronous computations that should be awaitable".

It also will be consistent with async function and will request the same approach for making blocks async: simply by adding word async at the start.

@boopathi
Copy link

boopathi commented Apr 3, 2016

#11 already has some discussions about this.

@domenic
Copy link
Member

domenic commented Apr 3, 2016

await means "wait next expression to be resolved" that conflicts with meaning of cycles. async means "next block has asynchronous computations that should be awaitable".

Right, that's exactly why it should be await, instead of async. Maybe the semantics are different in Python, but in JS, for await can only be used inside an async function, and it means "wait to execute the lines of code following the for await loop until the entire asynchronous loop is resolved".

@benlesh
Copy link

benlesh commented Apr 4, 2016

I agree with @domenic 100% here. When I see async I think of something that needs to be called to execute that will execute asynchronously. However await, to me, means "something on this line is async and we'll wait for it to proceed". for await makes total sense, where for async, to me, does not.

Also, it would be really confusing if we had some for async that magically made a block of code async inside of a non-async function. Which I think OP is implying he wants.

@alexeyraspopov
Copy link
Author

But what about consistency and not creating yet another standard? Python was somehow inspired by ECMAScript. So why can't we use their experience since they succeed in pushing async/await into language?

@zenparsing
Copy link
Member

@alexeyraspopov Thanks for jumping in here, and I definitely looked at Python's syntax when we discussed this.

await for/with would imply that something is awaiting for a completion of a for or with statement.

I agree with this, which is why we went with for await instead of await for.

The motivation for using await instead of async boils down to:

  1. The async keyword introduces an async context (where await may appear). The for await statement already must appear in an async context. It doesn't introduce a new one.
  2. With for await, we can identify all interleaving points within an async function by simply doing a textual search for "await".

But what about consistency and not creating yet another standard?

Agreement across languages is a good thing, certainly. I wasn't able to find a syntax across languages that was consistent however. Dart, for instance, proposes await for.

@benlesh
Copy link

benlesh commented Apr 4, 2016

The async keyword introduces an async context

Yes, this is what I was fumbling to say.

@bmeck
Copy link
Member

bmeck commented Aug 1, 2016

guessing this can be closed?

@alexeyraspopov
Copy link
Author

Yes

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants