I would like to consult may way of thinking.
Writing scraping bots or bots that perform certain activities on websites (using Puppeteer), I often need 'sort of' multithreading functionality, to be able to approach a number of pages at the same time and perform certain actions on them, preferably at the same time as well.
For this purpose, I use Promise.all() following this pattern:
const runInParallel = async(len) => {
// create an array with a number of elements corresponding to required number of functions
// to be performed at the same time
// these can also be URLs if I happen to know them beforehand
const iterations = [...Array(len).keys()];
// create an array of promises that run in parallel
return await Promise.all(
iterations.map(async i => {
try {
// use puppeteer to access a page, get data or perform certain actions
await scrape();
} catch (e) {
// handle error
} finally {
// close page and browser
}
})
);
}
Normally, the above is wrapped in yet another loop where each iteration awaits all the promises to be resolved/rejected before it starts the next iteration, this way i can access a number of pages at the same time, await all the actions to be completed on all of them and move to the next iteration where the process is repeated.
I am wondering what are the drawbacks of this approach and if there is a better alternative for the purpose of accessing a number of pages at the same time to either scrape data or perform certain actions on them.