Thursday, 1 June 2017

Sending results between promises in the promise pipeline

I am trying to learn and understand how it works when using "res =>" in the promise pipeline.

The first one findAllPrices works great, it has all the data and sends it to findAllPrices which returns even more data...

var findAllPrices = function([featuredArray, locationArray, newsArray, hotdealArray, priceArray]) {
    return new Promise((resolve, reject) => {
    // Do a lot of stuff with the data and create priceArray
    resolve({"featured":featuredArray, "location":locationArray, "news":newsArray, "hotdeal": hotdealArray, "prices": priceArray});
})};

Then comes the problem, when I come to return the data then res is "undefined"

How come it cannot send the res from the findAllPrices .then to the return the data .then

What am I missing or not understanding correctly?

Promise.all([findFeatured(), findLocation(), findNews(), findHotdeal()])
    .then(res => {
        console.log ("findAllPrices: " + JSON.stringify(res, null, 4))
        findAllPrices(res)
    })
    .then(res => {
        console.log ("return the data: " + JSON.stringify(res, null, 4));
        return({error:false,res})
    })
    .catch(err => {
        console.error(err);
        console.log("getFrontpage ERR: " + err);
        return({error:true,err})
    }
)



via torbenrudgaard

No comments:

Post a Comment