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

"for await" loop throws Syntax Error: Unexpected reserved word #21617

Closed
markbjerke opened this issue Jul 1, 2018 · 8 comments
Closed

"for await" loop throws Syntax Error: Unexpected reserved word #21617

markbjerke opened this issue Jul 1, 2018 · 8 comments
Labels
invalid Issues and PRs that are invalid. question Issues that look for answers.

Comments

@markbjerke
Copy link

markbjerke commented Jul 1, 2018

  • version 10.5.0
  • Platform Windows 10 Pro

This Code does not recognize "await" with Async Iterator and throws:

async function install(pkg) {
    function doIt() {
        return new Promise((resolve,reject) => {
            console.log('install: ' + pkg);
            installPkg(pkg, opts, (err)=> {
                err ? reject(err) : resolve(pkg);
            });
        });
    }
    return await doIt(); 
}

async function* installPackages(items) {
    for(let key of items) {
        let pkg     = key + '@' + globalDeps[key];
        let result = await install(pkg);
        yield result;
    }
}

try { 
    for await (let pkg of installPackages(items)) {
        console.log(pkg);
    }    
}
catch(e) {
    console.log(e.stack);
}

I was really excited about async generators and installed Node 10.5 but I can't get a thing to work? I tried the --harmony-async-iterator flag but that returns: unsupported flag?

@vsemozhetbyt
Copy link
Contributor

You can only use for-await-of loop inside an async function.

@ChALkeR ChALkeR added invalid Issues and PRs that are invalid. question Issues that look for answers. labels Jul 1, 2018
@ghost
Copy link

ghost commented Jul 1, 2018

@markbjerke

Node.js wrapper function that contains your module is not an async function (nor is it a generator). Identifier await is parsed as a keyword only in async functions, while it can be a standard variable in non-async functions.

If you want to use await as a keyword and don't want to declare separate named async function just to execute it, you can define async IIFE and put your code in it, like this:

(async () => {
  try{
    for await (let pkg of installPackages(items))
      console.log(pkg);
  }catch(e){
    console.log(e.stack);
  }
})();

If you're not sure whether something is a bug or not, I suggest posting in nodejs/help repo firstly next time.

@ChALkeR
Copy link
Member

ChALkeR commented Jul 1, 2018

@trvsapjiac something like this is one indentation level less, has less lines and is more readable:

async function main() {
  for await (let pkg of installPackages(items))
    console.log(pkg);
}

main().catch(e => console.error(e.stack));

Also, it has the wrapper function name in the stacktrace.

@ChALkeR
Copy link
Member

ChALkeR commented Jul 1, 2018

This does not look like it's an issue in Node.js.
It should have been asked in nodejs/help repo instead.

Also, I believe this question is answered.

I am going to close this.

Side notes:

@ChALkeR ChALkeR closed this as completed Jul 1, 2018
@markbjerke
Copy link
Author

Thank You - Next time I will try the nodejs/help repo first.

@8secz-johndpope
Copy link

bumped up with this using zmq + node v8.17 - switching to a newer version resolved this issue.

@x5engine
Copy link

thanks

@koki10190
Copy link

  • version 10.5.0
  • Platform Windows 10 Pro

This Code does not recognize "await" with Async Iterator and throws:

async function install(pkg) {
    function doIt() {
        return new Promise((resolve,reject) => {
            console.log('install: ' + pkg);
            installPkg(pkg, opts, (err)=> {
                err ? reject(err) : resolve(pkg);
            });
        });
    }
    return await doIt(); 
}

async function* installPackages(items) {
    for(let key of items) {
        let pkg     = key + '@' + globalDeps[key];
        let result = await install(pkg);
        yield result;
    }
}

try { 
    for await (let pkg of installPackages(items)) {
        console.log(pkg);
    }    
}
catch(e) {
    console.log(e.stack);
}

I was really excited about async generators and installed Node 10.5 but I can't get a thing to work? I tried the --harmony-async-iterator flag but that returns: unsupported flag?

Try this:

(async () => {
     //code here
})()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid Issues and PRs that are invalid. question Issues that look for answers.
Projects
None yet
Development

No branches or pull requests

6 participants