Skip to content

Generating __awaiter in all files even if no async/await is present #5564

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
tjoskar opened this issue Nov 8, 2015 · 0 comments
Closed

Generating __awaiter in all files even if no async/await is present #5564

tjoskar opened this issue Nov 8, 2015 · 0 comments
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@tjoskar
Copy link

tjoskar commented Nov 8, 2015

Hi,

If I compile the following code:

class Index {
    async someMethod() {
        await new Promise(resolve => resolve());
    }
}

It generates a helper function __awaiter and adds it to the output file, which is fine.

However, if I add another ts file, the output file will contain the __awaiter function as well, even if it doesn't contain any async/await method.

Exemple:

index.ts:

class Index {
    async someMethod() {
        await new Promise(resolve => resolve());
    }
}

a.ts:

let a;

Output:
index.js:

var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promise, generator) {
    return new Promise(function (resolve, reject) {
        generator = generator.call(thisArg, _arguments);
        function cast(value) { return value instanceof Promise && value.constructor === Promise ? value : new Promise(function (resolve) { resolve(value); }); }
        function onfulfill(value) { try { step("next", value); } catch (e) { reject(e); } }
        function onreject(value) { try { step("throw", value); } catch (e) { reject(e); } }
        function step(verb, value) {
            var result = generator[verb](value);
            result.done ? resolve(result.value) : cast(result.value).then(onfulfill, onreject);
        }
        step("next", void 0);
    });
};
class Index {
    someMethod() {
        return __awaiter(this, void 0, Promise, function* () {
            yield new Promise(resolve => resolve());
        });
    }
}

a.js:

var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promise, generator) {
    return new Promise(function (resolve, reject) {
        generator = generator.call(thisArg, _arguments);
        function cast(value) { return value instanceof Promise && value.constructor === Promise ? value : new Promise(function (resolve) { resolve(value); }); }
        function onfulfill(value) { try { step("next", value); } catch (e) { reject(e); } }
        function onreject(value) { try { step("throw", value); } catch (e) { reject(e); } }
        function step(verb, value) {
            var result = generator[verb](value);
            result.done ? resolve(result.value) : cast(result.value).then(onfulfill, onreject);
        }
        step("next", void 0);
    });
};
let a;

Is this by design?
This is not a big issue from my part but I'm working on a quite big project with lots of files and this adds about 604 B for each file (if I remove the spaces), and it adds a lot of noise.

My tsconfig:

{
    "compilerOptions": {
        "target": "es6",
        "module": "commonjs",
        "noImplicitAny": false,
        "removeComments": true,
        "preserveConstEnums": true,
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "sourceMap": true,
        "outDir": "./dist"
    },
    "exclude": [
        "node_modules"
    ]
}
$ tsc --version
> message TS6029: Version 1.8.0-dev.20151108

Thanks!

@mhegazy mhegazy added the Bug A bug in TypeScript label Nov 8, 2015
@mhegazy mhegazy added this to the TypeScript 1.8 milestone Nov 13, 2015
rbuckton added a commit that referenced this issue Nov 30, 2015
@rbuckton rbuckton mentioned this issue Nov 30, 2015
@mhegazy mhegazy added the Fixed A PR has been merged for this issue label Nov 30, 2015
@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 Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

3 participants