Search code examples
javascriptnode.jsexpresspromisebluebird

Waiting on multiple map methods to return using promises and then get all maps return values


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`
  });

Solution

  • 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`
      });