Reputation: 33
I want to call the queryDatabase function in parallel for each member in the resellers array, but cannot seem to find a way to do this given each call returns a value in the 'then' section...When I take away the await, the log prints empty because the promises didnt finish, but I believe the await makes it sequential
// TODO: MAKE PARALLEL
async function findOrgs(resellers) {
var ao = [];
for(const rese of resellers) {
await queryDatabase(rese)
.then((results) => {
ao = ao.concat(results.map(re => re._id));
})
}
console.log("done!", ao);
}
Is it possible to make this parallel?
Upvotes: 2
Views: 114
Reputation: 370689
Use Promise.all
instead:
async function findOrgs(resellers) {
const idArrs = await Promise.all(resellers.map((rese) => (
queryDatabase(rese)
.then(results => results.map(({ _id }) => _id))
)))
const ao = [].concat(...idArrs);
console.log("done!", ao);
}
Upvotes: 2