I am developing an app in Parse and I'm trying to understand promises. I'm not finding very many working examples other than the very simple ones here: https://parse.com/docs/js/guide.
I'm querying the _User table. Then I loop through the users in an _.each loop. I'm running 2 cloud functions inside the loop for each iteration. At what point do I create the promise? Do I create one for each cloud function success within the loop? Or do I push each success return value onto an array and make that the promise value outside of the loop? I've tried both but I can't figure out the correct syntax to do either, it seems.
I'll break it down in pseudo-code because that may be easier than actual code:
var query = new Parse.Query(Parse.User); query.find().then(function(users){
- loop through each user in an _.each loop and run a cloud function for each that returns a number.
- If the number > 0, then I push their username onto array1.
- Then I run a 2nd cloud function on the user (still within the _.each loop) that returns a number.
- If the number > 0, then I push their username onto array2.
}).then(function(promisesArray){
// I would like "promisesArray" to either be the 2 arrays created in the preceding section, or a concatenation of them.
// Ultimately, I need a list of usernames here. Specifically, the users who had positive number values from the cloud functions in the preceding section
- concatenate the 2 arrays, if they're not already concatenated
- remove duplicates
- send push notifications to the users in the array });
Questions: - At what point do I create & return promises & what syntax should I use for that? - Should .then(function(promisesArray){ be .when(function(promisesArray){ (when instead of then)?