maria
maria

Reputation: 159

More promises on each promise node.js/js

I have a function that have promises, and looping trough hospital, hotel, and gambling. Then it's currently merging and returning it.

What would be the best way to rewrite this function, so each of the promiseArrs results can be looped through and do some more promising?

 function GetOwnedFirms(userid) {
     var promiseArr = [
         hospital.find({userid: userid}),
         hotel.find({owner: userid}),
         gambling.find({userid: userid}),
     ];

     return Promise.all(promiseArr).then(function (results) {
         // Want to loop trough each results with a new promise etc..

// call funct1 promise to hospital, func2 to hotel, etc etc.
// then after everything is done, concat them and return.
         var OwnedFirms = results[0].concat(results[1], results[2]);
         return OwnedFirms;
     });
 }

Upvotes: 1

Views: 61

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1075925

If you want to loop through those results and do more operations resulting in promises, simply do so, and return the result of Promise.all on those operations out of your then callback:

function GetOwnedFirms(userid) {
    var promiseArr = [
        hospital.find({userid: userid}),
        hotel.find({owner: userid}),
        gambling.find({userid: userid}),
    ];

    return Promise.all(promiseArr).then(function(results) {
        return Promise.all(results[0].concat(results[1], results[2]).map(function(firm) {
            return doSomethingThatGeneratesAPromiseWith(firm);
        }));
    });
}

Remember that promise chains are pipelines, where each then or catch handler can transform the result travelling through, including with async operations. then and catch create new promises. If you return a promise out of a then or catch callback, the promise created by then or catch is resolved to the promise you return (meaning it will be fulfilled or rejected based on what that promise does). (More about that terminology on my blog if you're unfamiliar with any of it.)


Re your comments:

But i have to go trough each of them seperatly, hospital will have one kind of promise, hotel another etc. How would that work ?

...

But then i wonder how i would make each of the results into another promise, and then on final ( before returning it all), combine them :) cause 0 (hospital) have to do something different from hotel. Im worrying if i make it wrong. I need to have all the results new promises done before returning them and combing them.

As I said in the comments, you just apply the same principal to the individual results. The easiest way to do that is before your Promise.all:

function GetOwnedFirms(userid) {
    return Promise.all([
        hospital.find({userid: userid}).then(function(hospital) {
            return doSomethingAsyncWithHospital(hospital);
        }),
        hotel.find({owner: userid}).then(function(hotel) {
            return doSomethingAsyncWithHotel(hotel);
        }),
        gambling.find({userid: userid}).then(function(gambling) {
            return doSomethingAsyncWithGambling(gambling);
        })
    ]).then(function(results) {
        return results[0].concat(results[1], results[2]);
    });
}

See how we're still using the fact that promise chains are pipelines. We're transforming each of the three chains as we go along, then gathering them all together at the end (I assume you do want to do that; if not, just remove that final then handler).

Upvotes: 2

Related Questions