-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Needs Proposal: Async Iterators. #12755
Comments
I think this would be great. As a pattern Async Iterators are very useful, and something I use in my projects all the time. The ReactiveX guys wrote a JS polyfill/proof-of-spec over at https://github.com/Reactive-Extensions/IxJS. I re-wrote this as pure TypeScript at https://github.com/christyharagan/IxJS. The library itself includes operators over Iterators+AsyncIterators, and it would be awesome to replace the "polyfill" component with an official TS implementation. |
@christyharagan regenerator runtime has a working polyfill already. Now that async/await is out with the new compiler arch I think TS can transpile this sort of thing relatively easily. |
Awesome, thanks |
Async Iterators
Async iterators is a stage 3 proposal: https://github.com/tc39/proposal-async-iteration with intent to implement for Chrome and other browsers. This is a proposal for adding async iterators to TypeScript.
This means adding the async iterator interface as well as async generators statements and the
for - await
loop.(from the repo:)
An async iterator is much like an iterator, except that its
next()
method returns a promise for a{ next, done }
pair. As noted above, we must return a promise for the iterator result pair because both the next value and the "done" state of the iterator are potentially unknown at the time the iterator method returns.cc @domenic @zenparsing
Usage
I have a presentation I gave on it here:
https://docs.google.com/presentation/d/1r2V1sLG8JSSk8txiLh4wfTkom-BoOsk52FgPBy8o3RM/edit?usp=sharing where I build autocomplete using async iterators.
(from the repo)
The async iteration statement:
for
-await
-of
We introduce a variation of the
for-of
iteration statement which iterates over async iterable objects. An example usage would be:Async for-of statements are only allowed within async functions and async generator functions (see below for the latter).
During execution, an async iterator is created from the data source using the
[Symbol.asyncIterator]()
method.Each time we access the next value in the sequence, we implicitly
await
the promise returned from the iterator method.Async generator functions
Async generator functions are similar to generator functions, with the following differences:
next
,throw
, andreturn
) return promises for{ next, done }
, instead of directly returning{ next, done }
. This automatically makes the returned async generator objects async iterators.await
expressions andfor
-await
-of
statements are allowed.yield*
is modified to support delegation to async iterables.For example:
This function then returns an async generator object, which can be consumed with
for
-await
-of
as shown in the previous example.Status
This feature already works in Babel and has progressed as far as other supported features such as object spread (
{...obj}
). Python, Dart and other language already support async iteration.I think we should consider adding it to TypeScript.
The text was updated successfully, but these errors were encountered: