I was surprised I couldn’t find an example of this anywhere, Im sure there is one but I seem to be having a hard time finding one.
I have an array of links…
const array = ["www.google.com", "www.yahoo.com", "www.disney.com"];
and a function that takes a link and goes to the page and does some stuff and then closes with a resolve. I want to run the function using each array item but I don’t want the functions to run all at once.
I tried array.forEach((item) => { functionThatUsesArrayData(item) })
but this runs each scraper all at once.
In one function id like to be able to use an array of links and pass each link the the function but launch them one at a time…so when the first link is done being scraped, the next array item will be used in a function and be scraped.
function(arrayItem).then(()=> function(arrayItem2)).then(()=> function(arrayItem3);
Somthing like this…I feel like I should be able to do this with promises but I just cant seem to figure it out.
This is a small example…the real code I have is a web scraper that has an array of links to scrape but I dont want to have 10 pages of puppeteer up scraping at once, I need it to happen one after the other. Any ideas? Thank you!
async function functionThatUsesArrayData(link) {
const chromeOptions = {
headless: false,
slowMo: 60,
defaultViewport: null,
args: ["--no-sandbox", "--disable-setuid-sandbox"],
};
const promise = new Promise(async (resolve, reject) => {
const browser = await puppeteer.launch(chromeOptions);
const page = await browser.newPage();
await page.goto(link);
await browser.close();
resolve(setTimeout(() => {
console.log(" TASKS FINSIHED COMPLETED")
}, 3000))
})
console.log(promise)
}
functionThatUsesArrayData()```