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?
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:
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
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:If you're not sure whether something is a bug or not, I suggest posting in nodejs/help repo firstly next time.