2

Why does the following example not compile? Basically, the question is how to properly declare a async iterable closure.

class Test {
  async foo() {
    const c = async () => {
    };
    await c();
  }

  async * bar() {
    const c = async * () => {
    };
    yield * c();
  } 
}

The errors are:

...: error TS1109: Expression expected.
...: error TS1005: ';' expected.

My tsconfig.json:

{
  "compilerOptions": {
    "declaration": true,
    "lib": [
      "es2017",
      "dom",
      "esnext.asynciterable"
    ],
    "module": "commonjs",
    "target": "es2015",
    "sourceMap": true,
    "outDir": "out",
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
  },
  "include": [
      "src/**/*.ts"
  ]
}

If I change the example to the following, it works. I use a function instead of arrow syntax:

class Test {
  async foo() {
    const c = async () => {
    };
    await c();
  }

  async * bar() {
    const c = async function *() {
    };
    yield * c();
  } 
}
2
  • 2
    What error does it fail with? Commented Nov 19, 2017 at 15:05
  • Could you please show the content of your tsconfig.json? Commented Nov 19, 2017 at 15:41

2 Answers 2

2

Arrow function ()=>{} and regular function has a few differences which are in play here.

For example arrow function has no this binding.

Furthermore, documentation on generators clearly states that you have to use function syntax as it uses this for iteration.

This is why Async Generators are not going to support Arrow function

Sign up to request clarification or add additional context in comments.

Comments

2

There is no support for arrow-function using async-generators yet; i.e. const c = async *()=> {};.

As with Generators, Async Generators can only be function declarations, function expressions, or methods of classes or object literals. Arrow functions cannot be Async Generators.

https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-3.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.