Skip to content

Asynchronous Iterations #11326

Closed
Closed

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

  1. appsforartists commented on Nov 14, 2016

    @appsforartists

    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.

  2. mhegazy commented on Nov 14, 2016

    @mhegazy
    Contributor

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

    it does.

    animation

  3. imetallica commented on Feb 10, 2017

    @imetallica

    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.
        }
      }
    }
    
  4. ErikCupal commented on Feb 10, 2017

    @ErikCupal

    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.

  5. locked and limited conversation to collaborators on Jun 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

CommittedThe team has roadmapped this issueES NextNew featurers for ECMAScript (a.k.a. ESNext)FixedA PR has been merged for this issueSuggestionAn idea for TypeScript

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions