0

I am trying to subscribe to an Observable and store it in a variable

      this.waiverData = this.waiverService
        .searchForWaiver(this.waiverTypeId, this.policyNumber, this.dateOfLoss)
        .subscribe(data => {
          this.waiverData = data;

This is working to a point but the call is stopping at

    .subscribe(data => {

and jumping straight to the if statement without capturing data from the object. It then runs through the if statement and once checking all of the conditions, it jumps back to the subscription at

    this.waiverData = data;

and then returns the complete object with all the data that I need and is accessible. This happens too late.. Here is the complete function:

    standardWaiver: StandardWaiver = new StandardWaiver();
    // standardWaiver: StandardWaiver;
    // standardWaiver: any;
    dateOfLoss: Date = null;
    waiverData: any;

async  searchForDuplicateAndCAP() { 
    // the search for duplicate function will call the search for waiver in cap function
    if (this.waiverTypeId === 1)
    {
    this.waiverData = this.waiverService
    .searchForWaiver(this.waiverTypeId, this.policyNumber, this.dateOfLoss)
    .subscribe(data => {
      this.waiverData = data;
      }
     );

      if (this.waiverData.duplicateFound === true) {
        // console.log(this.duplicateFound, + 'if duplicate found')
        this.showIfWaiverExistsInXXXXX = true;
        this.showIfWaiverDataNotFoundInXXXXX = false;
      } 
      else if (stuff){
     }
      else {
}

Do you have any idea of how to make the call complete before the if statement or why it's not. Thank you in advance. This is an updated question

  • try removing `this.waiverData = this.waiverService...` assign? This seems unnecessary, just run the method without it `this.waiverService.searchForWaiver...` – Hoang Dao Nov 30 '20 at 08:05
  • Thank you @Houang Dao... I need this.waiverData to subscribe to an Observable from another function. It's working correctly but i'm jumping out of the subscription and into the if statement before the subscription is called. – Michael Todd Dec 01 '20 at 15:43

1 Answers1

0

I found a solution to accessing the variable datasource. You must run the function/method within the subscription in order to immediately have access to the data.

here is the link to the solution:

https://stackoverflow.com/a/52081230/9183130