Javascript 等待多个map方法使用承诺返回,然后获取所有map返回值

Javascript 等待多个map方法使用承诺返回,然后获取所有map返回值,javascript,node.js,express,promise,bluebird,Javascript,Node.js,Express,Promise,Bluebird,在一个express项目中,我有两个映射,它们都运行在一个Puppeter实例和两个返回数组中。目前,我使用Promise.all等待两个映射完成,但它只返回第一个数组的值,而不返回第二个数组的值。我如何做到这一点,使我得到这两个映射变量的结果 const games = JSON.parse(JSON.stringify(req.body.games)); const queue = new PQueue({ concurrency: 2 }); const f = games.map

在一个express项目中,我有两个映射,它们都运行在一个Puppeter实例和两个返回数组中。目前,我使用Promise.all等待两个映射完成,但它只返回第一个数组的值,而不返回第二个数组的值。我如何做到这一点,使我得到这两个映射变量的结果

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

all接受承诺数组作为参数。您需要将两个数组作为单个数组参数传递

return Promise.all(f.concat(s))
  .then(function(g) {
    console.log(g); //only returns `f` result, not the `s`
  });

all接受承诺数组作为参数。您需要将两个数组作为单个数组参数传递

return Promise.all(f.concat(s))
  .then(function(g) {
    console.log(g); //only returns `f` result, not the `s`
  });

无需使用PQUE,bluebird已经支持这种开箱即用的功能:

(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);
 })();
或者更正确,但代码更多(因此最后一次搜索不会等待):


无需使用PQUE,bluebird已经支持这种开箱即用的功能:

(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);
 })();
或者更正确,但代码更多(因此最后一次搜索不会等待):


啊,这是有道理的,谢谢你,这工作如预期!很高兴能帮上忙这很有道理,谢谢你,这一切都如期而至!很高兴能帮忙