Node: "for await" loop throws Syntax Error: Unexpected reserved word

Created on 1 Jul 2018  路  7Comments  路  Source: nodejs/node

  • 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?

invalid question

Most helpful comment

@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.

All 7 comments

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

@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.

@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.

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:

  • There is a top level await proposal, but it's currently non-standard (on stage-2). It's also targeted only on ESM (module goal), i.e. not CJS.
  • There is an open discussion at #21267 about top level await with CJS.

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

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

thanks

Was this page helpful?
0 / 5 - 0 ratings

Related issues

stevenvachon picture stevenvachon  路  3Comments

sandeepks1 picture sandeepks1  路  3Comments

addaleax picture addaleax  路  3Comments

danialkhansari picture danialkhansari  路  3Comments

willnwhite picture willnwhite  路  3Comments