2

I have myPeople function, that within it calls a promise function like this

var myPeople = function(){
    var go;
    return new Promise (function(resolve){
        User
           .getPeople()
           .then(function(allPeople){
                go = allPeople;
                //console.log(go)
                resolve(go);
           })
        })
    return go;
}

if I log my go within the block i get my objects, but i can not get it to return this object..

rweye
  • 83
  • 1
  • 6
  • 1
    You need to use the `then` method on the returned promise, not just on the `getPeople` method – Sterling Archer Oct 29 '14 at 18:42
  • Is there a way to get myPeople return the object itself as `{.. ..}` which i could use that way i dont have to do `myPeople().then( do something here );` ? – rweye Oct 29 '14 at 18:49
  • @goms no, there is none. This is because then there would be no way to know the method is in fact performing an asynchronous action. – Benjamin Gruenbaum Oct 29 '14 at 19:12
  • This sounds like a duplicate of either [What is the deferred antipattern and how do I avoid it?](https://stackoverflow.com/q/23803743/1048572) or [Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference](https://stackoverflow.com/q/23667086/1048572) – Bergi Oct 29 '14 at 20:03
  • @Bergi, thanks for the links http://stackoverflow.com/questions/23803743/what-is-the-deferred-antipattern-and-how-do-i-avoid-it is very useful one in view this question. – rweye Oct 29 '14 at 20:18

1 Answers1

2

Chain the promise, also - avoid the then(success, fail) anti pattern:

var myPeople = function(){
    return User.getPeople()
           .then(function(allPeople){ // 
                console.log(allPeople);  
                return allPeople.doSomething(); // filter or do whatever you need in
                                                // order to get myPeople out of 
                                                // allPeople and return it

           });
        });
}

Then on the outside:

myPeople.then(function(people){
  console.log(people); // this will log myPeople, which you returned in the `then`
});
Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
  • Ben, I get what you say,but on the outside, want to use the object returned from myPeople along with other things, that why i don't need to nest everything under myPeople.then(). – rweye Oct 29 '14 at 19:09
  • @goms a promise using function performing an asynchronous action _cannot_ return a value directly. It _must_ return a promise over that value and you must unwrap it. – Benjamin Gruenbaum Oct 29 '14 at 19:11
  • You might want to [read this answer](http://stackoverflow.com/a/16825593/1348195) to get the hang of asynchronisity in JS. – Benjamin Gruenbaum Oct 29 '14 at 19:12
  • Thanks, most of my difficulties was that, I thought I could get the value directly somehow, I hope have well grasped the concept now. Thanks. – rweye Oct 29 '14 at 19:21
  • @goms you're welcome, feel free to use the tag (also tag your question with [promise]) if you have any more questions - it is preferred to ask them here and not in the issue tracker. – Benjamin Gruenbaum Oct 29 '14 at 19:38
  • Will try to keep the issue tracker for clean/bugs. :) – rweye Oct 29 '14 at 19:57