# How to ensure one method is executed before executing another?

**URL:** <https://forum.ionicframework.com/t/how-to-ensure-one-method-is-executed-before-executing-another/185573>\
**Category:** Ionic Framework\
**Created:** [March 21, 2020, 5:56pm UTC](https://forum.ionicframework.com/t/how-to-ensure-one-method-is-executed-before-executing-another/185573 "2020-03-21T17:56:19Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sweg](https://avatars.discourse-cdn.com/v4/letter/s/9dc877/32.png) [@Sweg](https://forum.ionicframework.com/u/Sweg)\
**Post date:** [March 21, 2020, 5:56pm UTC](https://forum.ionicframework.com/t/how-to-ensure-one-method-is-executed-before-executing-another/185573/1 "2020-03-21T17:56:19Z")

</div>

I am reading in my current location using [Geolocation](https://ionicframework.com/docs/native/geolocation) correctly, & now want to render a [Leaflet](https://leafletjs.com/reference-1.6.0.html) map which centers around those co-ordinates.

The problem that I’m facing is that both `getLoc()` & `initMap()` are being called in `ngAfterViewInit()`.  
Even though I’m calling `getLoc()` first, it looks as my longitude & latitude variables aren’t being populated before the map is initiated.

Can someone please tell me how I can ensure my current location is read in before the map is initiated, so I can use those co-ordinates to center the map?

Here is `ngAfterViewInit()`:

```auto
ngAfterViewInit(): void {
        this.getLoc();
        this.initMap();
    }

```

Here is `getLoc()`:

```auto
getLoc() {
    this.geolocation.getCurrentPosition(
      {
        maximumAge: 1000, timeout: 5000,
        enableHighAccuracy: true
      }
    ).then((resp) => {
      this.lat = resp.coords.latitude
      this.lng = resp.coords.longitude
    }, er => {
      alert('Can not retrieve Location')
    }).catch((error) => {
      alert('Error getting location - ' + JSON.stringify(error))
    });
  }

```

And here is `initMap()`:

```auto
private initMap(): void {
    console.log(this.lat + " : " + this.lng);
    this.map = L.map('map', {
      center: [this.lat, this.lng],
      zoom: 3
    });
    const tiles = L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
      maxZoom: 19,
      attribution: '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'
    });
    tiles.addTo(this.map);
  }

```

---

<div class="post-metadata">

**Author:** ![rapropos](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/rapropos/32/100227_2.png) [@rapropos](https://forum.ionicframework.com/u/rapropos)\
**Post date:** [March 21, 2020, 6:15pm UTC](https://forum.ionicframework.com/t/how-to-ensure-one-method-is-executed-before-executing-another/185573/2 "2020-03-21T18:15:01Z")

</div>

Please read [this post](https://forum.ionicframework.com/t/ionic-app-loading-sequence/100109/11) for background.

You have written `getLoc` as if it were a type B method, but the crucial condition that is italicized is not true. `getLoc` is really a type C, and if you rewrite it accordingly, your problem will melt away.
