I've encountered an issue with my code that involves calling a function. Here is the snippet of code in question:
this.getAllOptions(questionID);
console.log("++++++++++++++++++++++++++++++++");
console.log(this.result);
The task of this function is straightforward - it invokes a service, which then returns an array of objects. What I specifically require from this returned data is just the "item.Content," formatted as a string. Below is the relevant code snippet:
result: string;
getAllOptions(question_ID){
this.result = "";
this._service.getOptionsQuestion(question_ID)
.subscribe( data => {
data.forEach(item => {
console.log(item.Content);
this.result += item.Content;
});
});
}
However, a problem arises where the lines of code after invoking the "getAllOptions()" function are executed prematurely. I am seeking a way for those subsequent lines to wait until the function has completed execution.
Is there a solution to accomplish this?