Here's the code snippet I'm working with:
getTotalBookListCost(bookList:string[]):number
{
let cost=0;
bookList.forEach(el=>{
this.store.doc("Books/"+el).get().subscribe(data=>{
let temp=<Book>data.data();
cost+=temp.cost;
},error=>this.toastr.error(error.message));
});
return cost;
}
The issue is that the value returned is always 0, most likely because the return statement is being executed before the forEach loop completes the Firebase calls. How can we make sure the function returns only after the loop has finished executing?