I have an array of arrays of promises, which I would like to keep nested because they are start and end location coordinates, so the two sets of coordinates should stay together in the inner arrays.
I am trying to resolve the promises but I can't seem to make it work. I'm trying to map the outer array to perform Promise.all on the inner arrays, but it's not giving me the desired outcome.
Here is my code:
const promises = idArr.map(route => route.map(id => (this.get(`places/${id}`))));
const places = promises.map(async route => (
await Promise.all(route)
));
Where promises
is:
[
[ Promise { <pending> }, Promise { <pending> } ],
[ Promise { <pending> }, Promise { <pending> } ],
[ Promise { <pending> }, Promise { <pending> } ],
[ Promise { <pending> }, Promise { <pending> } ],
[ Promise { <pending> }, Promise { <pending> } ],
[ Promise { <pending> }, Promise { <pending> } ],
[ Promise { <pending> }, Promise { <pending> } ],
[ Promise { <pending> }, Promise { <pending> } ],
[ Promise { <pending> }, Promise { <pending> } ],
[ Promise { <pending> }, Promise { <pending> } ]
]
This code gives the output:
[
Promise { <pending> },
Promise { <pending> },
Promise { <pending> },
Promise { <pending> },
Promise { <pending> },
Promise { <pending> },
Promise { <pending> },
Promise { <pending> },
Promise { <pending> },
Promise { <pending> }
]
I have tried different combinations of mapped Promise.all's but in my mind, this should be the way to make it work, but I'm obviously missing something.