Skip to content
Advertisement

How to pass array inputs as parameter to async function?

I got set of urls from txt file [which I am having in my local] which I got as array inputs with below code

fs.readFile('urls.txt', function(err, data) {
    if(err) throw err;
    var testurls = data.toString().split("n");  
});

Sample output when I do console.log(testurls) [ ‘https://sample1.com’ ‘https://sample2.com’ ]

How to pass the values to below function as parameter/argument to run scan on each url one after other as I am trying to some performance test using light house?

async function nav_to_site() {
    const home_url = testurls;
    const browser = await puppeteer.launch({
        headless: true,
        defaultViewport: null,
        executablePath: '/usr/local/bin/chromium',
        args: ['--headless', '--no-sandbox', '--remote-debugging-port=9222', '--disable-gpu'],
    });

(async () => {
    let browser = null;
    let page = null;

    try {
        browser = await nav_to_site();
        console.log('Running lighthouse...');
        page = (await browser.pages())[0];
        const report = await lighthouse(page.url(), {
            port: 9222,
            output: 'json',
            logLevel: 'info',
            disableDeviceEmulation: true,
            budgetPath: 'budget.json',
            chromeFlags: ['--disable-gpu', '--no-sandbox', '--disable-storage-reset']
        }, config);
        const json = reportGenerator.generateReport(report.lhr, 'json');
        const html = reportGenerator.generateReport(report.lhr, 'html');

}

Advertisement

Answer

Easy way? Use fs.readFileSync() to load that text file.

Harder way, but better if the file is large? Promisify the reading of the file so you can call it with await. Then use a loop to do your puppeteer thing.

const fs = require('fs');
const util = require('util');
const readFile = util.promisify(fs.readFile);

async function getUrlsFromFile {
  const contents = await readFile('urls.txt');
  return lines.toString().split('n')
}

async function nav_to_all_sites_one_by_one() {
    testurls = await getUrlsFromFile()
    /* avoid forEach here */
    for (let testurl of testurls) {
      const browser = await puppeteer.launch({
        headless: true,
        defaultViewport: null,
        executablePath: '/usr/local/bin/chromium',
        args: ['--headless', '--no-sandbox', '--remote-debugging-port=9222', '--disable-gpu'],
      });

      console.log('Running lighthouse on', testurl);
      page = (await browser.pages())[0];
      const report = await lighthouse(page.url(), {
            port: 9222,
            output: 'json',
            logLevel: 'info',
            disableDeviceEmulation: true,
            budgetPath: 'budget.json',
            chromeFlags: ['--disable-gpu', '--no-sandbox', '--disable-storage-reset']
        }, config);
      const json = reportGenerator.generateReport(report.lhr, 'json');
      const html = reportGenerator.generateReport(report.lhr, 'html');
    }
}
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement