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);
}