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 Functions #1781

Closed
wants to merge 112 commits into from
Closed

Async Functions #1781

wants to merge 112 commits into from

Conversation

rbuckton
Copy link
Member

Support for Async Functions, as per the proposal in #1664.

  • Adds a node factory and node tree visitor (factory.ts, factory.generated.ts)
  • Adds a general code generator and locals builder (generator.ts)
  • Adds a node rewriter for destructuring, spread element, and async functions (rewriter.ts)
  • Adds support for parsing async functions and the await keyword (scanner.ts, parser.ts)
  • Adds type check for await and async function return types
  • Adds call to rewriter for async functions during emit (emitter.ts)
  • Adds __awaiter and __generator helper functions to emit output (emitter.ts)

- fixed label fall-through
- added implicit return
- disallow 'with' in async
- fixed ASI ambiguity with 'async' by disallowing newline
- cleanup rewrite of ForInStatement
}

function checkAwaitableReturnType(node: SignatureDeclaration, returnType: Type): Type {
// an async function has a valid return type if the type has a construct signature that takes
Copy link
Contributor

Choose a reason for hiding this comment

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

When you say "the type has a construct signature...", you don't really mean the type. You mean the type resulting from resolving the name of that type as a value.

// - Explicit flags are set on contextFlags

var previousContextFlags = contextFlags;
contextFlags = (contextFlags & (ambientFlags | ~ParserContextFlags.ContextParameterFlags)) | explicitFlags;
Copy link
Contributor

Choose a reason for hiding this comment

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

I understand now. This is very ES6-y. I prefer the model where ambient flags are passed by just doing nothing, and flags are explicitly turned on and off. I know that what you've done here is more to the grammar, but for some reason I find it less intuitive.

@teobugslayer
Copy link

Hello @rbuckton,

I've tried to use your branch and found a compiler crash. Using this code:

async function read(): string {
    return "";
}

gives this output:

c:\Work\asyncpoc\node_modules\typescript\bin\tsc.js:10207
                    if (target.flags & 1)
                              ^
TypeError: Cannot read property 'flags' of undefined
    at isRelatedTo (c:\Work\asyncpoc\node_modules\typescript\bin\tsc.js:10207:31)
    at checkTypeRelatedTo (c:\Work\asyncpoc\node_modules\typescript\bin\tsc.js:10183:26)
    at checkTypeAssignableTo (c:\Work\asyncpoc\node_modules\typescript\bin\tsc.js:10167:20)
    at checkReturnStatement (c:\Work\asyncpoc\node_modules\typescript\bin\tsc.js:14272:29)
    at checkSourceElement (c:\Work\asyncpoc\node_modules\typescript\bin\tsc.js:15005:28)
    at Object.forEach (c:\Work\asyncpoc\node_modules\typescript\bin\tsc.js:74:30)
    at checkBlock (c:\Work\asyncpoc\node_modules\typescript\bin\tsc.js:13881:16)
    at checkSourceElement (c:\Work\asyncpoc\node_modules\typescript\bin\tsc.js:14986:28)
    at checkFunctionLikeDeclaration (c:\Work\asyncpoc\node_modules\typescript\bin\tsc.js:13863:13)
    at checkFunctionDeclaration (c:\Work\asyncpoc\node_modules\typescript\bin\tsc.js:13838:17)

I am using node 0.12.2 x64 on Windows 8.1 and the tsconfig.json is:

{
    "compilerOptions": {
        "module": "commonjs",
        "noImplicitAny": true,
        "removeComments": false,
        "preserveConstEnums": true,
        "sourceMap": true,
        "target": "ES6"
    },
    "files": [ "index.ts", "node.d.ts" ]
}

Hope this helps!

@rbuckton rbuckton mentioned this pull request May 8, 2015
@mhegazy
Copy link
Contributor

mhegazy commented Jun 19, 2015

closing this in favor of: #3078

@mhegazy mhegazy closed this Jun 19, 2015
@tinganho
Copy link
Contributor

@mhegazy does this mean async functions are only supported in ES6? Then why is that?

@rbuckton
Copy link
Member Author

@tinganho Downlevel support for async functions will likely end up in a separate PR. This one is extremely out of date.

@tinganho
Copy link
Contributor

Ok thanks for explanation @rbuckton . I really want this feature in at least ES5 too.

@nippur72
Copy link

@teobugslayer perhaps it's related to #3978, with the difference that one is in the master branch.

@coveralls
Copy link

Coverage Status

Changes Unknown when pulling d57fe72 on prototypeAsync into * on master*.

@rbuckton rbuckton deleted the prototypeAsync branch September 16, 2016 00:09
@microsoft microsoft locked and limited conversation to collaborators Jun 18, 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.

10 participants