0

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.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
moataz
  • 1
  • 3
  • change it to a promise - https://stackoverflow.com/questions/55599421/subscription-to-promise – Aaron Saunders May 15 '21 at 20:37
  • Check this tread. Hope this will help you. https://stackoverflow.com/questions/58300488/angular7-receive-api-data-before-running-next-function – Hash_Dew May 16 '21 at 16:03

0 Answers0