Correct way to handle asynchronous call & return in Typescript

tab1.page.ts

Get_Sim_Agents() {
       this.simConfig.Get_Sim_Agents()
                  .subscribe( ... all that stuff)
    }

ps. stringifying and then parsing the response seems a waste. Not sure why you are doing that.

the service:

 Get_Sim_Agents() : Observable<string>{

        return this.http.get(
            "http://" +
            this.simulation_orchestrator_ip_address +
            ":" +
            this.simulation_orchestrator_port +
            "/get_sim_agents"
        );
}

You either go for promises (async await) or observables (subscribe). You can mix them as you are trying in your code but that is a waste of your time, the app and the intellect of the people that invented both concepts. My opinion. :slight_smile:

And mostly if you have a service, every method of the service returns something. So the first word to type is return. Any method in the service that doesn’t, needs critical scrutiny or made private.

Pretty sure there is more to comment about the code, but this seems to be the core/first step of the answer to your question.

1 Like