I am trying to get data using Cheerio.js, and using async/await to synchronize requests.
Here is my (pseudo)code:
for(var i=1; i<=3; i++){
rp(url)
.then(async (html) => {
const $ = await cheerio.load(html);
dataItem1 = await $('input[id=data1]').val();
console.log(dataItem1);
await console.log("yay!" + i);
})
.catch((err) => {
console.log(err);
})
}
When I run it using node.js, I get the output as:
<required dataItem1>
Yay! 4
<required dataItem1>
Yay! 4
<required dataItem1>
Yay! 4
but what I want is
<required dataItem1>
Yay! 1
<required dataItem1>
Yay! 2
<required dataItem1>
Yay! 3
Where am I going wrong, and what changes should I make in the code to get the desired output?