Re:The Way of the Gopher: Making the Switch from Node.js to ...
by gotchange on Sunday March 27, 2016 @10:25AM
(#11370035)
I rewrote your async function making it more concise and compact. Isn't advisable not to use «for in» with arrays?
[ Parent
| on HN ]
Re:The Way of the Gopher: Making the Switch from Node.js to ...
by Arnavion on Sunday March 27, 2016 @03:31PM
(#11371149)
return arr.map(ele => await axis.get(ele));
is not legal - the await has no corresponding async. You may have wanted to write return arr.map(async ele => await axis.get(ele));
but that's the same as return arr.map(ele => axis.get(ele));
(Assuming axis.get returns a promise.)That is, it returns an array of promises of values, not a promise of an array of values like erlich's function.
erlich's example is correct, with the replacement of for-in with for-of.
Edit:
Also, a solution using Array.map() is not impossible:
return Promise.all(arr.map(ele => axis.get(ele)));
but this has different characteristics than the original function. The original ran the tasks in series whereas this runs them in parallel.