Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

210
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!