I have to calculate the total price from a bunch of products stored in firebase. Since in firebase each product will be returned separately, how can I know when the total is completely calculated?
For example:
var total = 0;
firebaseApp.database().ref('/products/').once('value')
.then(function(snapshot) {
var product = snapshot.val();
total += product.price;
});
If I have 12 products, the total can only be returned once all those products are retrieved from the database so it's not possible for me to use the variable total
until I'm sure all the products are fetched.