For my current app, I am getting a list of locations base on the user's current locations and the user's home address, so the two are different. I save them into two different geofire trees and want to query them at seperate times, but in the end, I would like to combine them together. So my data might look like this. My question is how do I combine the two data after the data are finished loaded, I am afraid if I do my actions outside of the ready clause, all of my data won't be ready, but if I put the action in either of the ready clause, the other one might not be ready. Thank you! -currentLocationGeo -user1 -lat:12.345 -lng:53.5435 -addressLocationGeo -user1 -lat:124.544 -lng:34.54
currentLocationGeo.on("key_entered", function (key, location, distance) {
});
addressLocationGeo.on("key_entered", function (key, location, distance) {
});
currentLocationGeo.on("ready", function () {
});
addressLocationGeo.on("ready", function () {
//do something to combine the two geo together and make sure everything is loaded.
finalGeo.push(eachLocation);
});
//do something to finalgeo, but I am not sure if finalGeo will actually be ready without being in the geo ready.
You need to use promises here. Angular do has it like $q service.
$q.all
does exaclty what you need, waits for finishing all promises. But if you have no promises, you may create 2 deferred objects and resolve them inside your ready callbacks, and wait after that $q.all().
var currentLocationdeferred = $q.defer();
var addressLocationdeferred = $q.defer();
currentLocationGeo.on("key_entered", function (key, location, distance) {
});
addressLocationGeo.on("key_entered", function (key, location, distance) {
});
currentLocationGeo.on("ready", function () {
currentLocationdeferred.resolve('any result');
});
addressLocationGeo.on("ready", function () {
addressLocationdeferred.resolve('any result 2');
});
$q.all([currentLocationdeferred.promise, addressLocationdeferred.promise]).then(function(result){
// result[0] == 'any result'
// result[1] == 'any result 2'
finalGeo.push(eachLocation);
})