-
Notifications
You must be signed in to change notification settings - Fork 164
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Async iterators #580
Comments
So in that proposed syntax if you want to define a method returning an async iterable you'd have to define a separate interface for that return type, and return that instead? Or would But either way, yes, I definitely want some way of being able to return async interables in specs. So whatever shape that takes, sounds good to me. |
@mkruisselbrink I'm not sure I understand, so this reply may be trash 😀
You'd have to define interface interface_identifier {
iterable<value_type>;
}; But the prose associated with Async iterables are complicated straight away as you need to deal with "in parallel" and task queueing, else there's no point in it being async, so you really need to a way to express how the values are "pulled". |
Maybe I'm mixing up async iterables with async iterators. Afaik a interface that specifies |
Restating:
In the OP, let sum = 0;
for await (const i of slowCounter) { sum += i; } Whereas @mkruisselbrink is after: let sum = 0;
for await (const i of someObject.getSlowCounter()) { sum += i; } (Aside: for sync With the OP proposal, you'd need to write: interface Whatever {
WhateverSomethingAsyncIterator getSomethings();
}
interface WhateverSomethingAsyncIterator {
async_iterable<Something>;
} Which, if you're thinking of this as an async replacement for Both use cases seem legitimate. I guess I would assume |
@inexorabletash cheers! I'm on the same track now. Unless I'm missing something, the |
There is already some intertwinedness, so I wouldn't worry too much about it. Alternative you could define some host hooks that HTML would fill in. |
I'm prototyping something here as part of WICG/kv-storage#6 which should probably get upstreamed eventually. Right now I'm manually pushing stuff into a queue, as does Jake's OP. Interestingly that's not really how async iterators work... they instead "pull", only running a set of steps each time next() is called, lazily. This gives built-in backpressure, which both of us are ignoring, I think. (Unless I misunderstood the "yield" in Jake's OP.) |
I was thinking "yield" would behave as it does in a generator, so the prose would pause until the next pull. |
It'd be nice if we could create async iterators in a similar way to iterators.
The prose would need to be a little more complicated than
iterable
, probably written like a generator.For example:
To asynchronously iterate over a
SlowCounter
, run the following steps:"Yield" would queue a task on the given task source to resolve the iteration's promise with the given value. The algorithm would not advance until the next iteration was requested.
I guess it's tricky as task sources are an HTML thing, but I can't see how it can be handled correctly otherwise.
The text was updated successfully, but these errors were encountered: