I have a controller which loads different attributes of my data via two separate http calls. An overly simplified example would be something like:
- A Country class which has attributes name, official language, and population
- one service (A) which returns countries and their languages
- another service (B) which returns countries and their populations
- let's assume that the set of countries returned in B is a subset of the countries returned in A, and I don't know either set ahead of time
- I want to end up with an array of Country objects - all countries returned by A, with their populations set (based on the data returned by B)
If the calls were synchronous, I would simply do something like:
- call service A and store Objects in a map keyed by Country name
- call service B, iterate over results: if a result in is the previous map, then update its population field
But of course I can't do that because the call to service B might return first, so the map is empty.
I have tried using promises via the "$broadcast"/"$on" functionality, but this doesn't work consistently because both calls are asynchronous (I broadcast when B is done, and try to apply the results to A, but sometimes A has not finished yet).
What should I do here?