Reputation: 1455
In an express project, I have 2 maps that both run through a puppeteer instance and both return arrays. Currently, I am using Promise.all to wait on both maps to finish, but it only returns the values from the first array and not the second. How can I do this so that I get both map variables results?
const games = JSON.parse(JSON.stringify(req.body.games));
const queue = new PQueue({
concurrency: 2
});
const f = games.map((g) => queue.add(async () => firstSearch(g.game, g.categories)));
const s = games.map((g) => queue.add(async () => secondSearch(g.game, g.categories)));
return Promise.all(f, s)
.then(function(g) {
console.log(g); //only returns `f` result, not the `s`
});
Upvotes: 0
Views: 413
Reputation: 276306
There is no need to use a PQueue, bluebird already supports this out of the box:
(async () => {
const games = JSON.parse(JSON.stringify(req.body.games));
let params = { concurrency: 2};
let r1 = await Promise.map(games, g => firstSearch(g.game, g.categories), params);
let r2 = await Promise.map(games, g => secondSearch(g.game, g.categories), params);
console.log(r1, r2);
})();
Or more correctly but with more code (so the last - first search doesn't wait):
(async () => {
const games = JSON.parse(JSON.stringify(req.body.games));
let params = { concurrency: 2};
let fns = [
...games.map(g => () => firstSearch(g.game, g.categories)),
...games.map(g => () => secondSearch(g.game, g.categories)),
];
let results = await Promise.map(fns, fn => fn(), params);
console.log(results);
})();
Upvotes: 1
Reputation: 281686
Promise.all accepts an array of Promises as an argument. You need to pass on both arrays as a single array argument
return Promise.all(f.concat(s))
.then(function(g) {
console.log(g); //only returns `f` result, not the `s`
});
Upvotes: 3