Skip to content

Async iteration generates invalid javascript when target is es2017 #15928

@jsedlacek

Description

@jsedlacek

TypeScript Version: 2.3.2, 2.4.0-dev.20170518

Code

async function run() {
  for await (const i of [1, 2]) {
    console.log(i);
  }
}

run();

Expected behavior:

Typescript generates valid javascript when target is es2017.

Actual behavior:

Typescript generates invalid javascript (notice yield outside of generator):

var __asyncValues = (this && this.__asyncIterator) || function (o) {
    if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
    var m = o[Symbol.asyncIterator];
    return m ? m.call(o) : typeof __values === "function" ? __values(o) : o[Symbol.iterator]();
};
async function run() {
    try {
        for (var _a = __asyncValues([1, 2]), _b = yield _a.next(); !_b.done; _b = yield _a.next()) {
            const i = _b.value;
            console.log(i);
        }
    }
    catch (e_1_1) { e_1 = { error: e_1_1 }; }
    finally {
        try {
            if (_b && !_b.done && (_c = _a.return)) yield _c.call(_a);
        }
        finally { if (e_1) throw e_1.error; }
    }
    var e_1, _c;
}
run();

My tsconfig.json is

{
  "compilerOptions": {
    "lib": [
      "es2017",
      "esnext.asynciterable",
      "dom"
    ],
    "target": "es2017",
    "module": "commonjs"
  }
}

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type
No fields configured for issues without a type.

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions