I just can't seem to get a hang of JavaScript promises.
In Parse, I have an attribute on a user that stores an array of Facebook friends IDs. When a new user logs in through Facebook for the first time I want to iterate through their Facebook friends that are using the app, then update those user's Facebook friends array to include this new user.
So... in user before_save I have this:
var friendsArray = user.get("facebookFriends");
// Iterate through all Facebook friends
_.each(friendsArray, function(facebookId){
// Retrieve the Facebook friend user
var query = new Parse.Query(Parse.User);
query.equalTo("facebookId", facebookId);
console.log("this executes");
query.find().then( function(){
var user = results[0];
// This does not execute
console.log("Need to update " + user.get("displayName") + "'s facebook friends");
return Parse.Promise.as();
});
}
My problem is not that different than another previous problem I encountered (Parse JavaScript SDK and Promise Chaining), except that this time I need the results of the async call before I can begin updating the user's Facebook friends array.