Looping async functions in nodejs
Clash Royale CLAN TAG#URR8PPP
Looping async functions in nodejs
so here's my problem. In the following function I want to get some data from a website using puppeteerjs. The function searches for a product on the website with the names its getting from a mongodb database. So here is my question. I want to loop the function through an array of names I'm getting from mongo. So if the function is completed once , it starts again with the next name in the array until there is nothing else in the array.
async function scrape()
const browser = await puppeteer.launch(
headless: false
);
const page = await browser.newPage();
await page.setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36');
await page.goto(dealer);
await page.type('.Search-bar-text-input', arrayOfArticles[i]);
await page.click('.Embedded-search-button')
await page.waitFor('.BuyingOptions-total-price');
const result = await page.evaluate(() =>
let path = '.BuyingOptions-total-price';
let price = document.querySelector(path).innerText;
return price;
);
So I originally wanted to simply do that with this:
for (var i = 0, len = arrayOfArticles.length; i < len; i++){
but from what I've read that doesnt work with async functions. How would I archive the same for my function?
that doesnt work with async functions
await
scrape
I already tried that and it still didnt work, how exactly would you code that?
– Alex Jones
Aug 10 at 7:05
(async () => for (let i = 0; i < arrayOfArticles.length; i++) await scrape(); )();
– CertainPerformance
Aug 10 at 7:06
(async () => for (let i = 0; i < arrayOfArticles.length; i++) await scrape(); )();
1 Answer
1
you can go with a slight modification
return page.evaluate(() =>
let path = '.BuyingOptions-total-price';
let price = document.querySelector(path).innerText;
return price;
);
//----------------
(async () =>
for (var i = 0, len = arrayOfArticles.length; i < len; i++)
const result = await scrape(arrayOfArticles[i]);
)()
@AlexJones doesn't it work?
– code-jaff
Aug 10 at 7:49
so I found the problem , there was something wrong with the database, the code you provided worked like a charm, thank you very much
– Alex Jones
Aug 10 at 8:08
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
that doesnt work with async functions
Sure it just, justawait
each call ofscrape
– CertainPerformance
Aug 10 at 7:01