Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

208
Vistas
For loop run puppeteer function out of order

I have a loop which contains an puppeteer function. The problem is that the loop is doing all puppeteer at the same time.

I've tried this:

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()
    })
}

Could you tell me what I'm doing wrong and how can fix this?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Its is an async function, you have to keep a look out at the created browser.

something like this should help you.

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

Have a look at the library I build, it will solve your issue https://www.npmjs.com/package/puppeteer-express

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda