How i can storage http respons?

I am working on a geolocation, google maps project. I wrote a simple API and uploaded my server. I can reach my api’s data with;

export class Tab2Page implements OnInit, AfterContentInit {
getLat: any;
getLong: any;
        constructor(private geolocation: Geolocation, public http: HttpClient) {
        this.http.get("apilink").subscribe(data => 
        {
          this.getLat = data["lat"];
          this.getLong = data["long"];
        });
       console.log(this.getLat); //gives undefined
      }
}

But when i want to reach getLat and getLong variable from any function - or outside this.http.get()- that variables give undefined. What should i do? I tried static, const, global, var declare etc.

ngAfterContentInit(): void {
console.log(this.getLat); //gives undefined
}

Some topics said that it depends on async problem. But i cannot fix this problem.

Adjust your expectations, for this is the way things are. Your intuition that asynchronous programming is involved is spot-on, but if you view this as a “problem”, you’re in for a lot of frustration.

It’s hard to give specific recommendations without knowing that you’re really trying to achieve with getLat - for example, if you’re trying to reference them from a template, then the AsyncPipe is a tool you might like.

In general, though, you need to incorporate in your designs the fact that Ionic applications, like most modern web apps, tend to prioritize responsiveness over predictability. The code inside your subscribe block will be executed when the underlying network request returns. Nothing outside that block has any clue when that will be unless you explicitly make the effort to tell it, and frequently that effort isn’t really worth it.

If you’re using this information to display a marker on a map, for example, do the things required to spawn the marker inside the subscribe block, so the user sees “no marker” and then “marker where it is supposed to be”. If you’re displaying it as text, think about a placeholder such as “loading…”. If the whole page simply can’t display at all until this data comes in, then think about popping a loading overlay or investigating route resolvers.

constructor{
        this.http.get(this.url).subscribe(
          data => {
            this.post = data;
          });
      }
 ngAfterContentInit{
console.log(this.post); //gives undefined
}

console.log(this.post); works before this.post = data; Is it possible to completely execute http.get before ngAfterContentInit?