-2

This is my simple App

for(var i = 0 ; i < prod.length;i++ ){
var urls = prod[i].URL
urls = urls.split(" ")[0];
scrapeIt(urls, {

   avatar: {
        selector: ".image img"
      , attr: "src"
    }
}).then(async ({ data, response })  =>  {
   // console.log(`Status Code: ${response.statusCode}`)
 urli = "https://www.choithrams.com"+data.avatar

 var name = prod[i].Name
 var price = prod[i].price

 console.log(name)
})
}

I want to get the result step by step but it is not iterating correctly. Secondly I, for example, I have a global variable outside and I want to overwrite its value from the one I get, I cannot simply do it.

the Result is

Mara Chopped Tomatoes Easy Open 400 gms
Mara Chopped Tomatoes Easy Open 400 gms
Mara Chopped Tomatoes Easy Open 400 gms
Mara Chopped Tomatoes Easy Open 400 gms
Mara Chopped Tomatoes Easy Open 400 gms
Mara Chopped Tomatoes Easy Open 400 gms

which is the last item in mineJSON. I am a beginner in all this kindly do help.

1 Answers1

0

It's better using Promise.all in this case:

Promise.all(prod.map(({ URL, Name, price }) => {
   const URL = URL.split(" ")[0];
   return scrapeIt(urls, {
      avatar: {
        selector: ".image img"
      , attr: "src"
      }
   }).then(async ({ data, response })  =>  {
     // console.log(`Status Code: ${response.statusCode}`)
     const urli = "https://www.choithrams.com" + data.avatar
     return {
       urli, Name, price
     }
   })
})).then(results => {
   // Results is an array of:
   // { urli: "...", Name: "...", price: ... }
   ...
})
Ionică Bizău
  • 109,027
  • 88
  • 289
  • 474