Search code examples
javascriptnode.jses6-promise

Promise.all() stops working before finishing


I've a very simple script that gets me some info by mapping over an array of around 150 records and the code seems to work fine with smaller number of records but everytime I run it with this 150 records it just stops working and doesn't continue and I think it might be a Promise.all problem.

any idea?

code:

const request = require('request');
const axios = require('axios');
const cheerio = require('cheerio');
const fs = require('fs').promises;

let champions = [];

const getChampData = async hrefs => {
  const requests = hrefs.map(async ({ href }) => {
    try {
      const html = await axios.get(href);
      const $ = cheerio.load(html.data);

      const champName = $('.style__Title-sc-14gxj1e-3 span').text();

      let skins = [];

      $('.style__CarouselItemText-sc-1tlyqoa-16').each((_, el) => {
        const skinName = $(el).text();
        skins.push(skinName);
      });

      const champion = {
        champName,
        skins
      };
      console.log(champion);

      return champion;
    } catch (err) {
      console.error(err);
    }
  });

  const results = await Promise.all(requests);

  await fs.writeFile('json/champions-skins.json', JSON.stringify(results));
  return results;
};

edit #1:

I used a package called p-map with it and now everything works just fine!

const axios = require('axios');
const pMap = require('p-map');
const cheerio = require('cheerio');
const fs = require('fs').promises;

const getChampData = async hrefs => {
  // const champions = JSON.parse(await fs.readFile('json/champions.json'));

  try {
    let champsList = await pMap(hrefs, async ({ href }) => {
      const { data } = await axios(href);

      const $ = cheerio.load(data);

      const champName = $('.style__Title-sc-14gxj1e-3 span').text();

      let skins = [];

      $('.style__CarouselItemText-sc-1tlyqoa-16').each((_, el) => {
        const skinName = $(el).text();
        skins.push(skinName);
      });

      const champion = {
        champName,
        skins
      };

      console.log(champion);

      return champion;
    });
    await fs.writeFile(
      'champions-with-skins-list.json',
      JSON.stringify(champsList)
    );
  } catch (err) {
    console.error(err.message);
  }
};

Solution

  • On Error return is missing. Look like issue with some url to fetch.

    const getChampData = async hrefs => {
      const requests = hrefs.map(async ({ href }) => {
        try {
          const html = await axios.get(href);
          // rest of the code 
        } catch (err) {
          console.error(err);
          return []
        }
      });
    
      const results = await Promise.all(requests);
    
      await fs.writeFile("json/champions-skins.json", JSON.stringify(results));
      return results;
    };