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.
or any better way to get the information from the observable because i am having a lot of issues from it
another example of the issue
in the service:
getItemByIdd(id : number) : items {
this.items.subscribe( (data)=>{
for(let i =0;i<data.length;i++ ){
if(data[i].idd==id){
this.tempItesm=data[i];
alert(this.tempItesm.name);/////// the alert shows the right name (1)
return this.tempItesm; }
} } );
return this.tempItesm;
}
The class i want to use the function in:
inc(id:string,idd:number){
this.titem= this.dBItems.getItemByIdd(idd);
/// i want the code here to wait for the function to execute
alert(this.titem.name); //// the alert shows undefined (2)
}
alert (2) comes before (1) is there any possible way to force the code to wait? i tried to do async/await but failed.
1 post - 1 participant