0

I have an array of resources called companies, and I run some query calls on another resource based on that, and then fire a callback. I tried the following, but the callback gets called straight away:

$q.all(companies.map(function(company) {
  return Person.query({
    companyId: company.id
  });
})).then(function(people) {
  // do stuff
});

http://plnkr.co/edit/OxasetGYU7xwojUNreiA?p=preview

3
  • Does Person.query() really return a promise? What is the value of people when the callback fires too early?
    – Bergi
    Aug 13, 2014 at 23:12
  • If you'd show us the code for Person.query, we could help you debugging.
    – Bergi
    Aug 13, 2014 at 23:13
  • @Bergi its the standard $.resource query method. I have added an example. I also tried with $q.when and it has the same issue.
    – Petah
    Aug 13, 2014 at 23:28

1 Answer 1

1

You need to provide a promise array to $q.all to have it wait for the underlying promise(s) to be fulfilled, use the $promise property returned by the resource.

$q.all(companies.map(function(company) {
  return Person.query({
    companyId: company.id
  }).$promise; //<-- Here
})).then(function(people) {
  console.log(people);
});

Demo

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.