Skip to content

Async function __awaiter allows var declarations to clobber function parameters #20461

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

Closed
yeerkkiller1 opened this issue Dec 4, 2017 · 1 comment · Fixed by #21215
Closed
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@yeerkkiller1
Copy link

TypeScript Version: 2.6.2, 2.7.0-dev.20171203, target: es2015

Code

async function test(url) {
    console.log(url);
}
async function fn(url) {
    await test(url);
    var url;
}

fn("test");

Expected behavior:
"test" should be logged

Actual behavior:
undefined is logged

The generated code clobbers the var declaration in the outer function, by adding another function around the code:

var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
function test(url) {
    return __awaiter(this, void 0, void 0, function* () {
        console.log(url);
    });
}
function fn(url) {
    return __awaiter(this, void 0, void 0, function* () {
        yield test(url);
        var url;
    });
}
fn("test");
@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Dec 5, 2017
@rbuckton
Copy link
Contributor

rbuckton commented Dec 5, 2017

Looks like we need to hoist var declarations out of the generator function even in ES2015.

@mhegazy mhegazy added this to the TypeScript 2.7.1 milestone Jan 11, 2018
@mhegazy mhegazy added the Fixed A PR has been merged for this issue label Jan 16, 2018
@microsoft microsoft locked and limited conversation to collaborators Jul 3, 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

Successfully merging a pull request may close this issue.

4 participants