0

I'm learning about JS promises and have made some progress in my understanding of things, however unsure how to bring it together with return and using Q.all

So say I have a function: (getParentsForLocation returns a promise)

    function doBusiness() {

    return Q.all(
    locations.map(function(item, currentIndex) {
         return getParentsForLocation(item.id)
        .then(function(res) {
             return checkParent(res, currentIndex)
          }

        });
    }))
    .then(_.uniq(locations))
  }

Then following this i.e. after that map has gone through all elements in the locations array, I want to run something like underscore's uniq function: _.uniq(someArrayIHave);

  1. Do i need to place this in a Q.all([])? \
  2. If so, would it run each method in that array sequentially?
  3. I presume there is something I'd need to do with that doBusiness() function, e.g. return some promise, but unsure how that would look?

Any help appreciated.

Many Thanks.

userMod2
  • 8,312
  • 13
  • 63
  • 115
  • So you want to wait for all the created promises, and then somewhere else (i.e. not in `doBusiness(...)` function), you want to call `_.uniq(...)` on some other array, do you confirm? – sp00m Feb 06 '17 at 17:54
  • There is one array, I want ``doBusiness`` to work on that, then once its completed I want to call ``_.uniq`` – userMod2 Feb 06 '17 at 18:16

1 Answers1

1

Do i need to place this in a Q.all(…)?

Yes. Your map() call should get you an array of promises.

If so, would it run each method in that array sequentially?

No. Or at least, we don't know, they could do anything on the inside.

I presume there is something I'd need to do with that doBusiness() function, e.g. return some promise

Yes. From my promise rules of thumb: If a function does something asynchronous, it must return a promise. This is also true for your two callback functions.

How would that look?

function doBusiness() {
    return Q.all(locations.map(function(item, currentIndex) {
//  ^^^^^^ ^^^^^^
        return getParentsForLocation(item.id)
//      ^^^^^^
        .then(function(res) {
            return updateDB(res, currentIndex);
//          ^^^^^^
        });
    }));
//    ^
}
Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • Thanks - that makes sense - but where would I call ``_.uniq(myArray)`` - as ``myArray`` would be only be ready for that once ``doBusiness`` has completed? – userMod2 Feb 06 '17 at 18:24
  • Just place `.then(_.uniq)` after the promise that `Q.all()` got you, it will return another promise for the duplicate-free array – Bergi Feb 06 '17 at 18:28
  • I've updated my question with my full code above with your suggestions - does that look correct? – userMod2 Feb 06 '17 at 18:38
  • Nah, you need either `.then(function(locations) { return _.uniq(locations); })` or `.then(_.uniq)`; always pass a function to `then`. And your callback doesn't return anything (i.e. `undefined`) if `res` is empty, not sure if that is what you want. – Bergi Feb 06 '17 at 18:52
  • Ok, and finally to actually kickstart it all - do i need to place in a line of code like ``Q([doBusiness()])`` - because when i do i get ``state: pending`` – userMod2 Feb 06 '17 at 19:01
  • No, you need to do `doBusiness().then(function(result) { console.log(result) }, console.error);` – Bergi Feb 06 '17 at 19:09
  • Awesome - got it now - so the key take away point - need to actually ``return`` promises – userMod2 Feb 06 '17 at 19:28
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/134993/discussion-between-usermod2-and-bergi). – userMod2 Feb 06 '17 at 19:29