0

In the below code, the getElements function is not waiting for the getSChema() function to complete, it's running asynchronously, how do I make it synchronous function call, I want to wait until the getschema() function execution is completed

getSChema()
  {
    this.getPdiSchema(this.ApplicationsComponent.selectedApplication)
    .subscribe((res: any) => { 
      console.log("res:"+res);
     this.resdata=res;
            }
          );
  }

    getElements() {  
        let elements:any[]; 
          let complexType:any[];  
          let sequence:any[];  
          let elements1:any[]; 
          let elements2:any[]; 
          let complexType1:any[];  
          let sequence1:any[];   
          let elementsArray: PdiSchemaBase<any>[] = [];

    this.getSChema(); //wait until getSchema execution is complete

         elements =  this.resdata["xs:schema"]["xs:element"]
         complexType = elements[0]["xs:complexType"];
         sequence = complexType[0]["xs:sequence"];
         elements1 = sequence[0]["xs:element"];
         complexType1 = elements1[0]["xs:complexType"];
         sequence1 = complexType1[0]["xs:sequence"];
         this.pdiElements = sequence1[0]["xs:element"];  
         let i=1;
         for (let element of this.pdiElements)
         {
             console.log("printing elements:"+i)
           if(element["$"].type !== undefined)
           {
             console.log("element.type:"+element["$"].type)
               elementsArray.push(new TextboxElements({
               key: element["$"].name,
               label: element["$"].name,                       
               required: true,
               order: i
             }))
           }
            i=i + 1;
         }                  
      return elementsArray.sort((a, b) => a.order - b.order);
      }

If I add a Observable and subscribe to it, am not able to return the value from getElements

getElements()
{

      let elementsArray: PdiSchemaBase<any>[] = [];   
      console.log("calling get schema:");     
      console.log("this.selectedApplication:"+this.ApplicationsComponent.selectedApplication);   
      this.getPdiSchema(this.ApplicationsComponent.selectedApplication)
      .subscribe((res: any) => { 
       console.log("res:"+res);
       //code here....
        }
      );
         // this return doens't seems to work
         return elementsArray.sort((a, b) => a.order - b.order);
  }

2 Answers 2

1

It might be best to change getSChema() to return the observable.

getSChema() {
    return this.getPdiSchema(this.ApplicationsComponent.selectedApplication);
}

And then in getElements() you can subscribe to it

getElements() {
    ...
    this.getSChema().subscribe((res: any) => {
        // code to execute once it's completed
    })
}

Response to comment:

Remove getSChema() from getElements() and instead where you call getElements() use something along the lines of:

this.getSChema().subscribe((res: any) => {
    this.resdata = res;
    this.elements = this.getElements();
});
Sign up to request clarification or add additional context in comments.

2 Comments

if I add a Subscribe in getElements how do i return the value "return elementsArray.sort((a, b) => a.order - b.order);"
How are you calling getElements()? Are you doing something like this.elements = this.getElements()? Assuming something along the lines of this I'll update the answer
0

If you want to return return elementsArray.sort((a, b) => a.order - b.order); you can use map instead of subscribe, i.e return an observable and the method that expects that result can subscribe, so something in the line of....

getElements() {
  return this.getSchema()
    .map(res => {
      // do your stuff here, then....
      return elementsArray.sort((a, b) => a.order - b.order);
    });
}

And the function that is calling getElements...

myFunction() {
  this.getElements()
    .subscribe(data => console.log(data));
}

or you could maybe rather just call getSchema() directly, that I would do :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.