I am getting the data from firebase saving it in an observable then subscribing to it to extract the data.
In my service i subscribe to an observable to get the data using this method
canAddBookmark(item : bookmark){
let canAdd = true;
this.bookmarks.subscribe((data)=>{
alert("First!!!");
for(let i =0;i<data.length;i++){
if(item.idd==data[i].idd&&item.email==data[i].email){
canAdd=false;
return canAdd;
}
}
canAdd = true;
}
);
return canAdd;
}
Which should return a boolean to a function in another class the function:
addBookmark(idd:number){
let temp:bookmark = {idd:idd,email:this.email};
let val = this.bookmarkDb.canAddBookmark(temp);
alert("Second!!!!");
if(val==true){
alert("success");
this.bookmarkDb.addBookmark(temp);
}
if(val==false){
alert("Already book marked");}
}
the alert with "second" comes before "first". is there anyway to make the function wait for the other function to return the boolean.
i tried to play around with async /await i failed tho.