I am facing a challenge with running a selenium script in a loop to populate a database. I have an array of objects consisting of 57 items that need to be processed through the loop asynchronously. My goal is to iterate through each store, check its status, and save the data to the database. However, the issue arises when running asynchronous code within a synchronous loop, causing tasks to overlap without waiting for completion.
Here is a snippet from my array
module.exports = [{ id: '1031', store: 'CANNES - LA BOCCA' },
{ id: '1009', store: 'ST ANDRÉ-LES-VERGERS' },
{ id: '1046', store: 'MARSEILLE ST MENET' },
{ id: '1071', store: 'MARIGNANE' },
{ id: '1020', store: 'IFS' },
{ id: '1032', store: 'CORMEILLES-EN-PARISIS' },
{ id: '1044', store: 'CERGY' },
{ id: '1055', store: 'HERBLAY' }];
Check out my implementation
const chromeOptions = new chrome.Options().addArguments('disable-infobars', 'headless');
const chromeDesktop = {
prefs: {
profile: {
default_content_settings: {
images: 2,
},
managed_default_content_settings: {
images: 2,
},
},
},
};
const driver = new webdriver.Builder()
.withCapabilities(chromeDesktop)
.forBrowser('chrome')
.setChromeOptions(chromeOptions)
.build();
for (let index = 0; index < Stores.length; index++) {
(async function getStores(index) {
setTimeout(async () => {
try {
// Code snippet omitted for brevity
} finally {
await driver.quit()
}
}, 1000 * index);
}(index));
}
I resorted to using a temporary solution of the setTimeout function, but this approach is not optimal due to various factors affecting its functionality.
I came across suggestions to utilize the map function and Promise.all() to improve efficiency. However, I am unsure how to implement this effectively without overcomplicating the code. Any guidance on utilizing Promise.all() efficiently would be greatly appreciated.
Thank you