Promise.all with Firebase DataSnapshot.forEach
Solution 1:
You have to execute the Promise.all
after you filled the array with promises, i.e. inside the then
callback:
function loadMeetings(city,state) {
//$('#meetingsTable').empty();
return ref.child('states').child(state).child(city).once('value').then(function(snapshot) {
var reads = [];
// ^^^^^^^^^^^^^^
snapshot.forEach(function(childSnapshot) {
var id = childSnapshot.key;
var promise = ref.child('meetings').child(id).once('value').then(function(snap) {
// The Promise was fulfilled.
}, function(error) {
// The Promise was rejected.
console.error(error);
});
reads.push(promise);
});
return Promise.all(reads);
// ^^^^^^^^^^^^^^^^^
}, function(error) {
// The Promise was rejected.
console.error(error);
}).then(function(values) {
console.log(values); // [snap, snap, snap]
});
}