Tengo un bucle que contiene una función de titiritero. El problema es que el bucle está haciendo todo el titiritero al mismo tiempo.
He intentado esto:
import puppeteer from "puppeteer" (async function(){ for(let i = 0; i < 3; i++){ await launchPup().then(() => { }); } })() async function launchPup() { puppeteer.launch({headless: false}).then(async browser => { await browser.newPage() await browser.close() }) }¿Podría decirme qué estoy haciendo mal y cómo puedo solucionarlo?
Es una función asíncrona, debe observar el navegador creado.
algo como esto debería ayudarte.
var totalOpenedbrowser = 0; var minBrowser = 2; const wait = () => new Promise((resolve) => setTimeout(resolve, 200)) (async function() { for (let i = 0; i < 3; i++) { await launchPup().then(() => {}); } })() async function launchPup() { while (totalOpenedbrowser > = minBrowser) await wait(); // wait until there is a free slott totalOpenedbrowser++; puppeteer.launch({ headless: false }).then(async browser => { await browser.newPage() await browser.close() totalOpenedbrowser--; }) }Eche un vistazo a la biblioteca que construyo, resolverá su problema https://www.npmjs.com/package/puppeteer-express