Skip to content

Using await inside inline async function doesn't pass constructor arguments correctly (target ES5) #12262

Closed
@stevehansen

Description

@stevehansen

TypeScript Version: 2.1.1-rc

Code

class Test {
    constructor(public name: string) {
        console.log("in ctor:", name);
    }
}

const getValue: () => Promise<string> = () => Promise.resolve("test");
const doWork = async () => new Test(await getValue());
doWork().then(test => {
    console.log("test:", test);
    console.log("name:", test.name);
});

Expected behavior:
Expected the result of getValue to be passed as value to constructors' name parameter.

Actual behavior:
The constructor is called with undefined.

The generated javascript uses the following state machine:

var doWork = function () { return __awaiter(_this, void 0, void 0, function () { var _a, _b; return __generator(this, function (_c) {
    switch (_c.label) {
        case 0:
            _a = Test.bind;
            return [4 /*yield*/, getValue()];
        case 1: return [2 /*return*/, new (_a.apply(Test, [_c.sent()]))()];
    }
}); }); };

The result of getValue() is passed to the call to get the constructor (_a.apply) and the actual constructor is called with no arguments (the () at the end)

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptDomain: TransformsRelates to the public transform APIFixedA PR has been merged for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions