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

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

Closed
stevehansen opened this issue Nov 15, 2016 · 2 comments
Assignees
Labels
Bug A bug in TypeScript Domain: Transforms Relates to the public transform API Fixed A PR has been merged for this issue

Comments

@stevehansen
Copy link

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)

@stevehansen
Copy link
Author

If you do the await first and assign it everything works correctly.

const doWork2 = async () => {
    const value = await getValue();
    return new Test(value);
};

@stevehansen stevehansen changed the title Using await inside inline async function doesn't pass constructor arguments correctly Using await inside inline async function doesn't pass constructor arguments correctly (target ES5) Nov 15, 2016
@mhegazy mhegazy added Bug A bug in TypeScript Domain: Transforms Relates to the public transform API labels Nov 15, 2016
@mhegazy mhegazy added this to the TypeScript 2.1.3 milestone Nov 15, 2016
@rbuckton
Copy link
Member

The issue is due to the fact we need to bind void 0 as the first argument. So while the following doesn't work:

new (_a.apply(Test, [_c.sent()]))()

This does work:

new (_a.apply(Test, [void 0, _c.sent()]))()

@mhegazy mhegazy added the Fixed A PR has been merged for this issue label Nov 15, 2016
@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 Domain: Transforms Relates to the public transform API Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

3 participants