1

I have two functions and whenever I call them in ngOninit the second one always gets called before the other could someone explain how can I use async/await in such case with a clear and simple example

getLocation() {
    if (navigator.geolocation) {
      navigator.geolocation.getCurrentPosition((position: Position) => {
        if (position) {
          console.log("Latitude: " + position.coords.latitude +
            "Longitude: " + position.coords.longitude);
          this.lat = position.coords.latitude;
          this.lng = position.coords.longitude;
          console.log(this.lat);
          console.log(this.lat);
        }
      },
        (error: PositionError) => console.log(error));
    } else {
      alert("Geolocation is not supported by this browser.");
    }
  }
}

so im trying to get the location first and then call the getWeatherByCordinates method

getWeatherByCoordinates(){ 
    this.weatherService.getWeatherByLatAndLong(this.latitude,this.longitude).subscribe((res) =>{
      console.log('res',res);
    });
  }

in the ngOninit like so

ngOnInit() {
    this.getLocation();
    this.getWeatherByCoordinates();
}
2
  • Please share the code you got so we can help you Commented Sep 7, 2021 at 22:21
  • just did, i think the problem is at the navigators getCurentPosition method, because since i cant subscribe to the method i can use latitude and longitude only inside the scope of the method, when i call the service inside of it it works just fine Commented Sep 7, 2021 at 22:54

1 Answer 1

3
getLocation() {
    return new Promise((resolve, reject) => {
      if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition((position: Position) => {
          if (position) {
            console.log("Latitude: " + position.coords.latitude +
              "Longitude: " + position.coords.longitude);
            this.lat = position.coords.latitude;
            this.lng = position.coords.longitude;
            console.log(this.lat);
            console.log(this.lat);
            resolve({
              lat:position.coords.latitude,
              lng:position.coords.longitude
            })
          }
        },
          (error: PositionError) => reject(error));
      } else {
        alert("Geolocation is not supported by this browser.");
      }
    });
  }



async ngOnInit() {
    await this.getLocation();
    this.getWeatherByCoordinates();
}

Or Can use callback for this case

getLocation(callback) {
    if (navigator.geolocation) {
      navigator.geolocation.getCurrentPosition((position: Position) => {
        if (position) {
          console.log("Latitude: " + position.coords.latitude +
            "Longitude: " + position.coords.longitude);
          this.lat = position.coords.latitude;
          this.lng = position.coords.longitude;
          console.log(this.lat);
          console.log(this.lat);
          callback()
        }
      },
        (error: PositionError) => console.log(error));
    } else {
      alert("Geolocation is not supported by this browser.");
    }
  }
}

ngOnInit() {
        this.getLocation(this.getWeatherByCoordinates());
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.