1

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;
     });
 }
1
  • 3
    What do you mean by "do some more promising"? What do you want to be able to do? Commented Sep 29, 2017 at 12:27

1 Answer 1

2

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).

Sign up to request clarification or add additional context in comments.

3 Comments

Nice idea. But i have to go trough each of them seperatly, hospital will have one kind of promise, hotel another etc. How would that work ?
oh yeah. 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. If that made sence?
@maria: I've added to the end above. Again, it's just applying the pipeline concept in the right place. HTH.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.