Ben Wheeler
Ben Wheeler

Reputation: 7344

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

?

Upvotes: 6

Views: 7770

Answers (1)

thefourtheye
thefourtheye

Reputation: 239443

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

Upvotes: 14

Related Questions