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

Async arrow functions generate broken code when targeting es5 #7706

Closed
nevir opened this issue Mar 27, 2016 · 2 comments
Closed

Async arrow functions generate broken code when targeting es5 #7706

nevir opened this issue Mar 27, 2016 · 2 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@nevir
Copy link

nevir commented Mar 27, 2016

TypeScript Version: Tested on 1.8.9 and Version 1.9.0-dev.20160327

Code

let foo = async (_arg) => {};

Compiles to the following (when {"target": "es5"}):

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());
    });
};
var foo = function (_arg) __awaiter(this, void 0, void 0, function* () { });
@Arnavion
Copy link
Contributor

The compiler also gives an error:

foo.ts(1,11): error TS1057: An async function or method must have a valid awaitable return type.

It's by design that the compiler may emit some output (which may be garbage) even if there's an error. If you don't want the compiler to emit on error then use noEmitOnError: true

Edit: And note that the error is somewhat misleading since the real reason is that async functions aren't supported when targeting ES5.

@yortus
Copy link
Contributor

yortus commented Mar 28, 2016

I also reported this a month ago (#7334).

It's not specific to async arrow functions, it's when the async function is an expression (e.g. RHS of assignment) rather than a declaration.

@Arnavion that error TS1057 is due to an internal check that relies on the existence of Promise (which is not in ES5 by default). #7334 defines Promise, which unmasks the real problem - that no compiler errors are raised for async function expressions in ES5 even though the generated code is unusable.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

4 participants