Skip to content

Enable await in ES5 and ES2015 script mode#5852

Merged
mhegazy merged 3 commits into
microsoft:masterfrom
holtwick:feature/await_for_es5
Dec 2, 2015
Merged

Enable await in ES5 and ES2015 script mode#5852
mhegazy merged 3 commits into
microsoft:masterfrom
holtwick:feature/await_for_es5

Conversation

@holtwick

@holtwick holtwick commented Dec 1, 2015

Copy link
Copy Markdown
Contributor

Even though strictly generators are an ES6 feature the real world support is large enough to use the feature in well known environments like node.js or Electron app.

Since the previous output was not working at all anyway it feels like a good compromise to at least emit working code while still having the warning in place.

Even though strictly generators are an ES6 feature the real world support
is large enough to use the feature in well known environments like
node.js or Electron app. Since the previous output was not working at
all anyway it feels like a good compromise to at least emit working code
while still having the warning in place. The user would also need to add
"use strict" on top of her .ts file to make it work with node.js.
@msftclas

msftclas commented Dec 1, 2015

Copy link
Copy Markdown

Hi @holtwick, I'm your friendly neighborhood Microsoft Pull Request Bot (You can call me MSBOT). Thanks for your contribution!

This seems like a small (but important) contribution, so no Contribution License Agreement is required at this point. Real humans will now evaluate your PR.

TTYL, MSBOT;

@holtwick holtwick changed the title Enable await in ES6 and ES2015 script mode Enable await in ES5 and ES2015 script mode Dec 1, 2015
Comment thread src/compiler/emitter.ts Outdated

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is the actual change. Sorry, my IDE modified some whitespace ;)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please change the condition to just if (isAsync) and i would appreciate it if you fix the indentation issue.

@lbguilherme

Copy link
Copy Markdown

I disagree. When targeting ES5, only ES5 should be emitted, not ES5.5. This is related to the issue #4692, Granular Targeting.

Your best bet would be to target ES6 and use babel to transpile just what you need to (transpile classes, but leave generators as is).

@holtwick

holtwick commented Dec 1, 2015

Copy link
Copy Markdown
Contributor Author

@lbguilherme thanks for pointing to #4692. Indeed some target specific code emission sounds reasonable to me. Using Babel or the like instead as a second pass sounds like overkill and probably adds another source of failures for the end user.

As I mentioned before, the transpiler currently emits completely invalid code anyway. So this change would not harm and the error is still presented in my minimal change.

To illustrate what actually changes in the emitted code. This would be TS:

await foo();

With ES5 option the current implementation would emit:

yield foo();

This code can fail for 2 reasons: 1. Because yield is not supported or 2. because the wrapping function would need too look like function *bar().

Instead with this patch applied you get the same output as for ES6, which is:

return __awaiter(this, void 0, Promise, function* () {
    yield foo();
});

The __awaiter and everything else is already emitted anyway.

IMHO it would be great to have that little change, because for projects only targetting node.js, Electron or CEF this is a huge win.

@mhegazy

mhegazy commented Dec 1, 2015

Copy link
Copy Markdown
Contributor

@holtwick, stupid question, why are you emitting to ES5 and not ES6 if you are running on node v4+ or Electron?

@holtwick

holtwick commented Dec 1, 2015

Copy link
Copy Markdown
Contributor Author

@mhegazy, some ES6 features are not supported by node or Chrome/CEF/Electron right now, like default parameters function x(a=1, b=2) to name one. The TS es6 flag is all or nothing.

@mhegazy

mhegazy commented Dec 1, 2015

Copy link
Copy Markdown
Contributor

not sure this is the right solution though. we make no guarantees in the case of errors. the correct fix is to address #4692. i am open to taking this PR, but i do not think it solves the issue, it just kicks the can down the road until you run into the next blocking issue.

@holtwick

holtwick commented Dec 1, 2015

Copy link
Copy Markdown
Contributor Author

As I see from https://github.com/Microsoft/TypeScript/wiki/Roadmap async/await support is on the roadmap for 2.0 anyway? That's great news.

I agree that my patch is just a quick fix and that a solution in the context of #4692 or adding some other kind of other flag would be a more elegant and conform solution. Although this little patch would already get most use cases satisfied without any major drawbacks, since the errors are still in place. So if you could merge it, I would be very grateful.

If you like to I can elaborate more on the topic and present a follow up PR. For example the next issue down the road would be the use of async keyword with arrow functions ;)

@mhegazy

mhegazy commented Dec 1, 2015

Copy link
Copy Markdown
Contributor

ok then, I had one comment, please update the PR.

@holtwick

holtwick commented Dec 1, 2015

Copy link
Copy Markdown
Contributor Author

Done. Thanks a lot.

mhegazy added a commit that referenced this pull request Dec 2, 2015
Enable await in ES5 and ES2015 script mode
@mhegazy mhegazy merged commit 19d7e62 into microsoft:master Dec 2, 2015
@mhegazy

mhegazy commented Dec 2, 2015

Copy link
Copy Markdown
Contributor

thanks!

@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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants