Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Ryan Morales
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Ryan Morales
Full Stack JavaScript Techdegree Graduate 33,933 Points

Using promises and promise all with asynchronous calls to return an array of data.

I am attempting to scrape product urls from a page then use those urls to visit each products page and scrape information for the products. I am trying to create a Promise chain but I'm a beginner with Promises. The hang up is trying to return the product prices as an array after visiting each product url. Right now my code is just returning an array of undefined values. I have tried many combinations of for loops, prototype map, promises and Promise all. I can't figure it out. Any help would be appreciated. code below:

const request = require('request');
const cheerio = require('cheerio');
const fs = require('fs');
const rp = require('request-promise');

// Thanks to chovy @ stack overflow 
//https://stackoverflow.com/questions/21194934/node-how-to-create-a-directory-if-doesnt-exist
if (!fs.existsSync("./data")) {
    fs.mkdirSync("./data");
}

let shirturlscrape = new Promise((resolve) => {
    let mikeshirturl = 'http://www.shirts4mike.com/shirts.php';
    request(mikeshirturl, (error, response, html) => {
        if (!error) {
            const $ = cheerio.load(html);
            let shirtlinks = [];
            $('.products').filter(function() {
                let data = $(this);
                data.find('li a').each(function(i) {
                    shirtlinks.push($(this).attr('href'));
                });
                let shirturls = [];
                shirtlinks.map((i) => {
                    let url = 'http://www.shirts4mike.com/' + i;
                    shirturls.push(url);
                }) // end shirtlink map
                resolve(shirturls);
            }) // end products filter
        } // end error check
    }); // end request url

});

let shirtPrices = [];
let getPrices = (html) => {
    return new Promise((resolve)=>{
      const $ = cheerio.load(html);
        $('.shirt-details').filter(function(){
            let data = $(this);
            let price = data.find('.price').text();
            resolve(price);
        })  
    })

}


shirturlscrape
    .then((result)=>{
        Promise.all(result.map((i)=>{
            rp(i)
            .then(getPrices)
        }))
        .then((result)=>{
            console.log(result)
        })

})

1 Answer