rweye
rweye

Reputation: 83

Using bluebird promise

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

Upvotes: 2

Views: 422

Answers (1)

Benjamin Gruenbaum
Benjamin Gruenbaum

Reputation: 276596

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`
});

Upvotes: 2

Related Questions