简体   繁体   中英

Using an async function inside a loop

I'm using a javascript library named puppeteer, I have an asyncronous function for searching all iframes inside a page (and other things) like this:

function check_page(web_page){
    (async () => {
        const browser = await puppeteer.launch();
        const page = await browser.newPage();
        await page.goto(web_page);
       
        /*.   my code  */

        await browser.close();

  
})();
}

I have another function that I used to read a csv file with the list of most popular websites, for every site I have to call the previous function with the string of it like parameter:

function readCSV(csv){

  var lines=csv.split("\n");
  var result = [];
  var headers=lines[0].split(",");
  for(var i=0;i<lines.length;i++){
      //console.log("lines: "+lines[i])
      var obj = {};
      var currentline=lines[i].split(",");
      console.log("currentline: "+currentline[1])  
      check_page("https://www."+currentline[1]). // pass the site to the function like: https://www.itsname...
      
  }

}

But this doesn't work. It works sometimes for the last website of the list, but in general it gives this error:

UnhandledPromiseRejectionWarning: Error: Protocol not supported.
at exports.XMLHttpRequest.send (/Users/francesco/node_modules/xmlhttprequest/lib/XMLHttpRequest.js:299:15)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:1228) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:1228) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

My file.js has the following structure:

const puppeteer = require('puppeteer');

const fs = require('fs')
const fileContents = fs.readFileSync('./popular_website.csv').toString()
function readCSV(csv){
     // previous code
}
function check_page(web_page){
    // previous code
}
readCSV(fileContents) 

EDIT: I change my function but it doesn't work, it works only in the last website. I post the entire function:

async function check_page(web_page){

    const browser = await puppeteer.launch();
    const page = await browser.newPage();
    await page.goto(web_page)
    
    /* I search every iframe tag inside web-page and then I send a request for eachone of that for reading csp and x-frame-option from the header*/
    for (const frame of page.mainFrame().childFrames()){

      
      if(frame.url().toString() == "about:blank"){
        console.log("blank")
      }
      else{
        /* I  send for every iframe an http request for retrieve the policies from http header */
        var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;       
        var req = new XMLHttpRequest();
        req.open('GET', frame.url(), false);
        //req.send(null)
        var headers = req.getAllResponseHeaders().toLowerCase();       
        var arr = headers.trim().split(/[\r\n]+/);
            // Create a map of header names to values
            var headerMap = {};
            arr.forEach(function (line) {
              var parts = line.split(': ');
              var header = parts.shift();
              var value = parts.join(': ');
              headerMap[header] = value;
            });
        console.log("policy of:"+frame.url());
        console.log("CSP: "+headerMap["content-security-policy"]);
        console.log("x-frame-options: "+headerMap["x-frame-options"]);
        console.log("-----------------------------------------------------------------")
      }    
    } 
    await browser.close();
  
}

Calling a promise in a for loop is not always a good idea.
You expose yourself to weird side effects since you don't control the timing of your application.
Try to group your calls in a Promise.all():

async function check_page(web_page){
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto(web_page);
       
  /*.   my code  */

  await browser.close();
}

function readCSV(csv){

  var lines=csv.split("\n");
  var result = [];
  var headers=lines[0].split(",");
  Promise.all(
    lines.map(line => {
      var obj = {};
      var currentline = line.split(",");
      console.log("currentline: "+currentline[1])  
      return check_page("https://www."+currentline[1])
    })
  ).then(() => console.log('It worked')).catch(err => /* catch any error in Promise.all*/);
}

Check also your check_page function. It seems there's a protocol error that prevent your promise to resolve.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM