I am working with puppeteer and this is the abstract concept I want to resolve:
From an array of URLs, find all images and get BoxModel
// URLS, easy step
const urls = ['google.com', 'amazon.com', 'github.com', 'so on'];
// This is basically a browser tab from chromiumn a.k.a. puppeteer
const page = const page = (await browser.pages())[0];
// Add magic function
magic1(page) // all those listen from different page events
magic2(page)
magic3(page)
imageMagic(page) // this one wait for 'load' event
// inside imageMagic function
page.on('load', async function () { // wait untill load to find images
try {
const imgs = await page.$$('img');
for await (const itm of imgs) {
// DO things with each image
}
} catch (e) {
console.error(e);
}
});
Then the main process is some near to below:
async function ultraPowerMagicFunction(url){
try {
await magic(page)
await magic2(page)
await magic3(page)
await imageMagic(page)
const navigate = page.waitForNavigation({
waitUntil: 'load',
});
await page.goto(url);
await navigate;
// Here I need to wait until imageMagic finish
// nextExecution()
} catch (e){
console.error('PROBLEM IN MAIN FLOW:', e);
}
}
Now as final step, I need to do this for each item
for await (cont url of urls) {
await ultraPowerMagicFunction(url);
}
In resume, how to wait until event triggered (load
) finish their async method before go to next iteration?.
Thanks in advance.