'HERE Map geocoding not assigning to global variables (Angular)

I'm trying to take a predetermined address, and get the latitude and longitude from HERE Map API in my Angular application. I think I have an improper understanding of asynchronous methods, but I'm having a hard time figuring out how to fix it. When I run the method, the variables are assigned inside of it, but outside of the method they are still undefined.

var service = this.platform.getSearchService();
    let addrString: string = this.restaurant.address + ", " + this.restaurant.city + ", ON, Canada";
    service.geocode({
      q: addrString
    }, (result) => {
      this.lati = result.items[0].position.lat;
      this.lngi = result.items[0].position.lng;
    });

    console.log(this.lati);
    console.log(this.lngi);

The two console.logs return undefined. However, if I were to console.log inside of the service.geocode callback, then the proper values are returned. How can I get these values assigned properly?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source