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

260
Views
Puppeteer page.goto(url) inside looping not waiting until load

I have urls of pdf file. ex:

const filesUrl= ["http://example.com/animal.pdf", "https://example.com/fruit.pdf", "http://example.com/stars.pdf"]

I want to download that files and save it to my server folder using puppeteer.

What im doing is:

  1. looping filesUrl
  2. first loop im using page.goto(url)
  3. write file using fs.writeFile
  4. next loop

But i have error: [ERR_INVALID_CALLBACK]: Callback must be a function. Received undefined

If i look at the browser, the looping is not waiting until the page is fully loaded.

this is my code:

    const filesUrl = await page.$$eval("li.b_algo h2 a", urls => {
        return urls.map(url => url.href)
    })

    // filesUrl = ["http://example.com/animal.pdf", "https://example.com/fruit.pdf", "http://example.com/stars.pdf"]

    for (const fileUrl of filesUrl) {
        try {
            const filepage = await page.goto(fileUrl)

            await fs.writeFile(Math.random() + ".pdf", await filepage.buffer())
        } catch (error) {
            console.log('errorsadasd:', error);
        }
    }


    await browser.close()
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

The error is not related to the puppeteer, but to the fs.writeFile, which requires a callback as an argument, which is missing, i.e. undefined, hence the error.

Try adding the callback:

    await fs.writeFile(Math.random() + ".pdf", await filepage.buffer(), () => {
        console.log('done writing file');
    });
about 4 years ago · Juan Pablo Isaza Report

0

Try using writeFile, but wrapped in a promise:

import fs from 'fs';
const fsPromises = fs.promises;
const writeFilePromise = fsPromises.writeFile;

await writeFilePromise(Math.random() + ".pdf", await filepage.buffer());
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!