使用带有Puppeteer的Async / Await在数组上进行映射以返回结果

时间:2018-10-16 20:59:53

标签: javascript node.js asynchronous async-await puppeteer

我是异步编程的新手,所以这里可能缺少一些简单的东西。

我有一个快递项目,我在请求的正文中传递了一个数组。

在我的函数中,我验证主体,然后解析数组,并在映射到数组时使用Promise。

const games = JSON.parse(JSON.stringify(req.body.games));

const gamesMap = games.map((game) => gameSearch(game));

return Promise.all(gamesMap)
    .then(function(g) {
        // async is still running here, I want to wait until it returns 
        console.log(g); // returns [ undefined, undefined, ... ]
    });

游戏搜索功能使用puppeteer来使用无头浏览器来返回以数组形式传递的游戏价格。但是,它不会等到返回数组之后才调用Promise.all,因此上面的console.log(g);返回了一个未定义的数组。我想这与在gameSearch函数中使用async await有关,尽管我不确定在这里应该做什么?任何帮助将不胜感激。

function gameSearch(game) {
  (async () => {
    const url = '.....' + game;
    try {
      const browser = await puppeteer.launch();
      const page = await browser.newPage();
      await page.setUserAgent('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36');
      await page.goto(url);
      const selector = '.searchRcrd';
      await page.waitForSelector(selector);

      const searchRcrds = await page.$$(selector);

      const records = [];

      for (let i = 0; i < searchRcrds.length; i++) {
        const searchRcrd = searchRcrds[i];
        const title = await searchRcrd.$eval('h1', (h1) => h1.innerText.trim());
        const buyFor = await searchRcrd.$eval('.desc .prodPrice div:nth-child(2) .priceTxt:nth-child(1)', (buy) => buy.innerText.trim());
        const inStoreFor = await searchRcrd.$eval('.desc .priceTxt:nth-child(2)', (inStore) => inStore.innerText.trim());
        const imgSrc = await searchRcrd.$eval('div.thumb > a > img', (img) => img.src.trim());

        records.push({
          'title': title,
          'buyFor': buyFor,
          'inStoreFor': inStoreFor,
          'imgSrc': imgSrc
        });
      }

      await browser.close();

      return records;
    } catch (err) {
      next(err);
    }
  })();
}

1 个答案:

答案 0 :(得分:1)

return records(async () => {…})(); IIFE返回。删除它,使gameSearch本身成为一个async function,它返回数组(对数组的承诺)。

async function gameSearch(game) {
  const url = '.....' + game;
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.setUserAgent('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36');
  await page.goto(url);
  const selector = '.searchRcrd';
  await page.waitForSelector(selector);

  const searchRcrds = await page.$$(selector);

  const records = [];

  for (let i = 0; i < searchRcrds.length; i++) {
    const searchRcrd = searchRcrds[i];
    const title = await searchRcrd.$eval('h1', (h1) => h1.innerText.trim());
    const buyFor = await searchRcrd.$eval('.desc .prodPrice div:nth-child(2) .priceTxt:nth-child(1)', (buy) => buy.innerText.trim());
    const inStoreFor = await searchRcrd.$eval('.desc .priceTxt:nth-child(2)', (inStore) => inStore.innerText.trim());
    const imgSrc = await searchRcrd.$eval('div.thumb > a > img', (img) => img.src.trim());

    records.push({
      'title': title,
      'buyFor': buyFor,
      'inStoreFor': inStoreFor,
      'imgSrc': imgSrc
    });
  }

  await browser.close();

  return records;
}