Monday 12 June 2017

Node.js - How to return after all asynchronous calls have finished

As shown bellow, I am pushing the object link_to_json returns into an array allShirts declared in html_to_json.

However, the console.dir on the third last line and the return value of html_to_json logs an array of undefined references. Which I presume is because console.dir and return is executed before link_to_json functions finished.

How do I ensure the return value of html_to_json is a filled up allShirts array?

//Go to individual links and scrape relevant info
const link_to_json = (link) => {
    request(link, (err, res, body) => {
        if (!error_handler(err, res, link)) {
            const $ = cheerio.load(body);
            const shirt_detail = $('.shirt-details').find('h1').text();

            const Title = shirt_detail.substr(shirt_detail.indexOf(' ') + 1);
            const Price = shirt_detail.substr(0, shirt_detail.indexOf(' '));
            const ImageURL = $('.shirt-picture').find('img').attr('src');
            const URL = link;

            return new Shirt(Title, Price, ImageURL, URL);
        } else return {};
    });
}

//Crawl through all individual links listed in Root
const html_to_json = body => {
    const allShirts = [];
    const $ = cheerio.load(body);

    $('.products').find('a').each((index, val) => {
        allShirts.push(link_to_json(rootURL + $(val).attr('href')));
    });

    console.dir(allShirts); // <--- HERE
    return allShirts;
}



via Ja. L

No comments:

Post a Comment