Repository navigation
Asynchronous Iterations #11326
Description
The asynchronous iterations proposal is now in stage 3, so it should be safe to support that in TypeScript now. Currently, a function may be a generator or an async function, but it cannot be both. The asynchronous iteration proposal removes this restriction. Here's an example from their readme:
async function* readLines(path) {
let file = await fileOpen(path);
try {
while (!file.EOF) {
yield await file.readLine();
}
} finally {
await file.close();
}
}Types
Generator functions declare several interfaces:
interface IteratorResult<T> {
done: boolean;
value: T;
}
interface Iterator<T> {
next(value?: any): IteratorResult<T>;
return?(value?: any): IteratorResult<T>;
throw?(e?: any): IteratorResult<T>;
}
interface Iterable<T> {
[Symbol.iterator](): Iterator<T>;
}
interface IterableIterator<T> extends Iterator<T> {
[Symbol.iterator](): IterableIterator<T>;
}Async generators return promises instead of their IteratorResult, and use a new symbol, Symbol.asyncIterator:
interface IteratorResult<T> {
done: boolean;
value: T;
}
interface AsyncIterator<T> {
next(value?: any): Promise<IteratorResult<T>>;
return?(value?: any): Promise<IteratorResult<T>>;
throw?(e?: any): Promise<IteratorResult<T>>;
}
interface AsyncIterable<T> {
[Symbol.asyncIterator](): AsyncIterator<T>;
}
interface AsyncIterableIterator<T> extends AsyncIterator<T> {
[Symbol.asyncIterator](): AsyncIterableIterator<T>;
}Emit
I guess that this would require another helper function to downlevel to ES2015, similar to the one used for async functions. Yield and await expression would both be replaced by yield expression, so they need to be decorated with some tag, like yield ['await', promise] for await promise.
for-await-of
The proposal also introduces a new loop. An example from their readme:
for await (const line of readLines(filePath)) {
console.log(line);
}This iterates over the [Symbol.asyncIterator]() object and awaits the value of .next(), similar to a for-of loop. This loop can only be used in async functions.
Activity
appsforartists commentedon Nov 14, 2016
FWIW, TypeScript currently emits garbage if you try to
for await. Same code works in BabelI'd expect it to throw an error until async iteration is properly supported.
mhegazy commentedon Nov 14, 2016
it does.
imetallica commentedon Feb 10, 2017
One suggestion I think can make the asynchronous workflows easier to read:
await, we could do as F# does and uselet!,yield!anddo!.Put the
async{}inside instead of outside the function.For example
ErikCupal commentedon Feb 10, 2017
Iuri L. Machado (@imetallica) Eeh, I don't think it's a good idea.
The main goal of Typescript is to provide type safety and analysis for JS. Therefore, it shouldn't deviate from ECMAScript standard if it is possible.