regenerator
regenerator copied to clipboard
"await" within array comprehension returns a parse error
This async function tries to return an array of values concurrently resolved by different Promises (i.e. an equivalent for Promise.all()
):
async function demo() {
try {
let things = ['foo', 'bar', 'baz'];
let promises = things.map((thing) => returnAPromise(thing));
let results = [for (promise of promises) await promise];
console.log('posted many docs simultaneously, results are ', results);
} catch (err) {
console.log(err);
}
}
This fails with an error:
Error : Parsing file /.../index.js: Unexpected token (40:38)
My current workaround is to build up the results
array like this:
let results = [];
for (let promise of promises) {
results.push(await promise);
}
I have a live example of the code here. To reproduce, just check out the code and do npm i && npm run build
.
Thanks in advance, and let me know if this is just an error in my understanding or not in Regenerator.