Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse: Promise.when with many promises?

Parse documentation ( https://www.parse.com/docs/js/symbols/Parse.Promise.html#.when ) explains that when using Parse.Promise.when, it is kosher to specify an array of promises:

var p1 = Parse.Promise.as(1);
var p2 = Parse.Promise.as(2);
var p3 = Parse.Promise.as(3);

var promises = [p1, p2, p3];
Parse.Promise.when(promises).then(function(r1, r2, r3) {
  console.log(r1);  // prints 1
  console.log(r2);  // prints 2
  console.log(r3);  // prints 3
});

...which is sweet!

But, do you really have to list every single promise response in your then() function? Not really feasible if you have an array of promises of unknown size, and not very DRY!

Can I do this?

Parse.Promise.when(promises).then(function(responses) {
  console.log(responses[0]);  // prints 1
  console.log(responses[1]);  // prints 2
  console.log(responses[2]);  // prints 3
});

?

like image 531
Ben Wheeler Avatar asked Jul 21 '14 16:07

Ben Wheeler


1 Answers

You can make use of JavaScript's builtin special variable, arguments like this

Parse.Promise.when(promises).then(function() {
  console.log(arguments[0]);  // prints 1
  console.log(arguments[1]);  // prints 2
  console.log(arguments[2]);  // prints 3
});
like image 143
thefourtheye Avatar answered Nov 02 '22 23:11

thefourtheye