I have a pg object being returned with URLs. I need to parse the URLs and synchronously execute a function on each URL. The function can take upwards of 2-3 minutes to execute since it is running a LightHouse test.
I'm testing with 3 URLs. With the code below, all 3 URLs execute concurrently. This is not ideal since LightHouse results can become false positives if multiple pages are loading simultaneously.
What changes need to be made to make each url execute a lighthouse test synchronously?
var objectsFromPg = lighthouse.getRecordsFromPg();
objectsFromPg.then(pgResults => {
for ( var x = 0; x < pgResults.rows.length; x++){
lighthouse.launchChromeAndRunLighthouse(pgResults.rows[x].url)
}
})
launchChromeAndRunLighthouse: async function(url, flags = {}, config = null){
console.log("running lighthouse test for ", url)
flags = [
'--headless',
'--allow-running-insecure-content',
'--enable-automation'
]
return await chromeLauncher.launch(flags).then(chrome => {
flags.port = chrome.port;
return lighthouse(url, flags, config).then(results =>
chrome.kill().then(() => results));
});
}
You need to await the async lighthouse function. Eg:
objectsFromPg.then(async (pgResults) => {
for ( var x = 0; x < pgResults.rows.length; x++){
await lighthouse.launchChromeAndRunLighthouse(pgResults.rows[x].url)
}
})