Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

203
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda