Before proceeding to other functions, it is crucial that my service is completed as I rely on its result. Despite trying various solutions, such as this one, I have not been successful.
Here is an example of the code:
The service being used:
getText(){ return this.http.get(this.textURL, {responseType: 'text'}); }
I called it within the ngOnInit lifecycle hook like this:
ngOnInit()
{
let spaceIndex=0;
this.readJsonService.getText().subscribe((data) => {
for (const line of data.split(/[\r\n]+/)){
spaceIndex=line.search(/\s/);
this.weakness.push({
CWE_ID: line.substring(line.search('CWE-'), spaceIndex),
weakness: line.substr(spaceIndex)
})
}
console.log(this.weakness.length)
console.log(this.weakness)
});
this.tryThis();
}
In the tryThis() function, I aim to display the result contained in the global interface 'weakness' with the following code:
console.log(this.weakness.length); console.log(this.weakness)
However, the tryThis() function executes first, displaying an empty array and indicating a length of 0, even while the service is still running. It then logs the array from the ngOnInit function. I am seeking assistance with this issue. Thank you in advance.