lloistborn lloistborn - 15 days ago 7
Node.js Question

How to access value inside promise?

I'm trying to learn to use promise.
In this case i want to make a web scrapper. I'm using

request-promise
inside this code.

Actually the scrapper is working fine. I have missunderstand how to use the return value inside
then()
. I am also have googled it but no luck and still get stuck. Here is my code.

let categories = scrapper.getCategories(promoUrl);
categories.then(function (val) {
let promoPerCategory = [];
for (let i = 0, len = val.length; i < len; i++) {
let json = {
category: val[i].category,
url: val[i].url,
promo: []
}

scrapper.getPromoPerCategories(val[i].url)
.then(function (allPromo) {
for (let j = 0, lenResult = allPromo.length; j < lenResult; j++) {
json.promo.push({
imageUrl: allPromo[j].imageUrl,
merchantName: allPromo[j].merchantName,
promoTitle: allPromo[j].promoTitle,
validUntil: allPromo[j].validUntil,
promoUrl: allPromo[j].promoUrl
});

console.log(json.promo[j]);
}

})
promoPerCategory.push(json);
}

result.send({ "promoPerCategory": promoPerCategory });
})


the
then()
inside function
getPromoPerCategories
is working fine, i print out the value using
console.log(json.promo[j])


But the problem is i want to put array from
json.promo[j]
into array
promoPerCategory
so that i can return it using
result.send({ "promoPerCategory": promoPerCategory })


Can you please help me to do the right way? any help will be very appreciated.

Result is now like this

{"promoPerCategory": [{"category":"Fashion","url":"https://","promo":[]}


object
promo
is empty array

Answer

Your first problem is that result.send is not waiting for the getPromoPerCategories result. To make that happen, result.send needs to be in a callback of the getPromoPerCategories promise. It also looks like you want to wait for all of your results to come back before calling result.send, so you should use Promise.all and wait on all of the getPromoPerCategories promises. For example:

categories.then(function (val) {
    let promoPerCategory = [],
        promoPerCategoryPromises = [],
    for (let i = 0, len = val.length; i < len; i++) {
        let json = {
            category: val[i].category,
            url: val[i].url,
            promo: []
        }

        var promise = scrapper.getPromoPerCategories(val[i].url)
        .then(function (allPromo) {
            for (let j = 0, lenResult = allPromo.length; j < lenResult; j++) {
                json.promo.push({
                    imageUrl: allPromo[j].imageUrl,
                    merchantName: allPromo[j].merchantName,
                    promoTitle: allPromo[j].promoTitle,
                    validUntil: allPromo[j].validUntil,
                    promoUrl: allPromo[j].promoUrl
                });
                console.log(json.promo[j]);
            }
            promoPerCategory.push(json);
        });

        promoPerCategoryPromises.push(promise);
    }
    Promise.all(promoPerCategoryPromises).then(function() {
      result.send({ "promoPerCategory": promoPerCategory });
    });
})
Comments