Skip to content
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

"for await" inside async functions #14151

Closed
LeviticusMB opened this issue Feb 17, 2017 · 2 comments
Closed

"for await" inside async functions #14151

LeviticusMB opened this issue Feb 17, 2017 · 2 comments
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@LeviticusMB
Copy link

With PR #11326 now merged, isn't this supposed to work?

TypeScript Version:
Version 2.3.0-dev.20170217

Node version
v6.9.1

Command line

tsc --target es6 --lib esnext --outFile for-await.js for-await.ts ; node for-await.js

Code

function delay(ms) {
    return new Promise((resolve, reject) => {
        setTimeout(resolve, ms);
    });
}

async function* numbers() {
    yield 1;
    await delay(500);
    yield 2;
    await delay(1000);
    yield 3;
    await delay(2000);
    yield 4;
}

(async () => {
    let n = numbers();
    console.log(await n.next());
    console.log(await n.next());
    console.log(await n.next());
    console.log(await n.next());
    console.log(await n.next());

    for await (let next of numbers()) {
        console.log(next);
    }
})();

Expected behavior:

{ value: 1, done: false }
{ value: 2, done: false }
{ value: 3, done: false }
{ value: 4, done: false }
{ value: undefined, done: true }
1
2
3
4

Actual behavior:

{ value: 1, done: false }
{ value: 2, done: false }
{ value: 3, done: false }
{ value: 4, done: false }
{ value: undefined, done: true }
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
...
@mhegazy mhegazy added the Bug A bug in TypeScript label Feb 18, 2017
@mhegazy mhegazy added this to the TypeScript 2.3 milestone Feb 18, 2017
@rbuckton
Copy link
Member

There are two underlying issues here:

  1. The for-await-of transformation isn't emitting the correct await downleveling for each step.
  2. The second is that you do not have Symbol.asyncIterator defined, and we are not reporting an error at runtime for this.

I'm working on a fix for (1) and part of (2) now. Afterwards you will need to ensure you have a shim for Symbol.asyncIterator, for example:

(<any>Symbol).asyncIterator = Symbol.asyncIterator || Symbol.for("Symbol.asyncIterator");

TypeScript does not currently plan to emit this shim itself, as it could collide with existing shim libraries.

@mhegazy mhegazy added the Fixed A PR has been merged for this issue label Feb 18, 2017
@LeviticusMB
Copy link
Author

Awesome, thanks! 👍

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

3 participants