Below are two versions of code that should do the same thing. X,Y and Z are angular $resource's . First example works as expected but on the second example the .then() fires before results are back. In this example there's no advantage in method 2 but in practice I want to build the actions[] dynamically.
I've seen examples of the second method on the web and also versions which built the array using the equivalent of just X.doSomething(a) - i.e. not the $promise. The latter doesn't seem correct to me but it doesn't work either.
Can anyone suggest a solution that works?
//This works
$q.all([
X.doSomething(a).$promise,
Y.doSomething(b).$promise,
]).then(function (result) {
});
//This doesn't
var actions = [];
actions.push(X.doSomething(a).$promise);
actions.push(Y.doSomething(b).$promise);
$q.all([actions]).then(function (result) {
//Here before results obtained
});
Should mention that in the second approach nothing hits the server - ever.