Skip to content

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

Closed
@yeerkkiller1

Description

@yeerkkiller1

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");

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions