Typescript: Asynchronous Iterations

Created on 3 Oct 2016  路  4Comments  路  Source: microsoft/TypeScript

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.

Committed ES Next Fixed Suggestion

Most helpful comment

@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.

All 4 comments

FWIW, TypeScript currently emits garbage if you try to for await. Same code works in Babel

I'd expect it to throw an error until async iteration is properly supported.

I'd expect it to throw an error until async iteration is properly supported.

it does.

animation

One suggestion I think can make the asynchronous workflows easier to read:

  • instead of using await, we could do as F# does and use let!, yield! and do!.
    Put the async{} inside instead of outside the function.

For example

function readLines(path) {
  async{
    let! file = fileOpen(path);

    try {
      while (!file.EOF) {
        yield! file.readLine();
      }
    } finally {
      do! file.close(); // Or something else as identifier.
    }
  }
}

@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.

Was this page helpful?
0 / 5 - 0 ratings