3 ms·
async function() { const {data} = axis.get('foo'), arr = ['foo', 'bar', 'baz']; return arr.map(ele => await axis.get(ele)); } I rewro
by gotchange 11y ago
async function() {
const {data} = axis.get('foo'),
arr = ['foo', 'bar', 'baz'];
return arr.map(ele => await axis.get(ele));
}
I rewrote your async function making it more concise and compact. Isn't advisable not to use «for in» with arrays?
- Arnavion 11y agoreturn 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.