Home > database >  How to detect the end of Promise.all
How to detect the end of Promise.all

Time:09-04

I am trying to get a response when all the promises are complete, every promise is a diferent process of web scraping. This is the output that I get for the following code as you can see all finished is the first output how to get in the end?

all finished
partial end
partial end
partial end

Code in NodeJs using puppeteer

const puppeteer = require("puppeteer");
const URLs = [
    'https://www.google.es/search?q=dog&sxsrf=ALiCzsaZ5RIpFrQHMAxy9uZ9vbCu2wDAlw:1662240805949&source=lnms&tbm=isch&sa=X&ved=2ahUKEwjp7ZPGyfn5AhVEgv0HHSbDC1oQ_AUoAXoECAIQAw&biw=1280&bih=576&dpr=2',
    'https://www.google.es/search?q=dog&sxsrf=ALiCzsaZ5RIpFrQHMAxy9uZ9vbCu2wDAlw:1662240805949&source=lnms&tbm=isch&sa=X&ved=2ahUKEwjp7ZPGyfn5AhVEgv0HHSbDC1oQ_AUoAXoECAIQAw&biw=1280&bih=576&dpr=2',
    'https://www.google.es/search?q=dog&sxsrf=ALiCzsaZ5RIpFrQHMAxy9uZ9vbCu2wDAlw:1662240805949&source=lnms&tbm=isch&sa=X&ved=2ahUKEwjp7ZPGyfn5AhVEgv0HHSbDC1oQ_AUoAXoECAIQAw&biw=1280&bih=576&dpr=2'
];

main();

async function main() {
    await scrapingUrls().then(console.log("all finished"));
}

async function scrapingUrls() {
    const prom = [];
    for (var i = 0; i < URLs.length; i  ) {
      prom.push(scrapInformation(URLs[i]));
    }
    return await Promise.all([prom]);
}

function scrapInformation(url) {
    return new Promise(async function(resolve, reject) {
      const browser = await puppeteer.launch()
      const page = await browser.newPage()
  
      await page.goto(url, {waitUntil: 'networkidle2'});
      
      await browser.close().then(function () {
        console.log('partial end');
        resolve();
      })
    });
  }

CodePudding user response:

This:

await scrapingUrls().then(console.log("all finished"));

is wrong. You should give a function to .then():

await scrapingUrls().then(() => console.log("all finished"));

Here the console.log("all finished") is executed right away.

Also:

return await Promise.all([prom]);

just prom without the brackets.

  • Related