Aync функция внутри другой асинхронной функции - PullRequest
0 голосов
/ 13 мая 2019

Я удивлен этим, поэтому хотел бы получить некоторые рекомендации о том, что я делаю здесь неправильно с использованием синтаксиса Async Await.

Функция 'getUserLocation' возвращает местоположение пользователя:

  getUserLocation = async () => {
    const response = await window.navigator.geolocation.getCurrentPosition(
      coord =>  ( coord.coords),
      err => {
        const { code, msg } = err;
        if (code === 1) {
          prompt(
            "Location is needed to get the local salah time, please try again.",
            msg
          );
        } else if (code === 2 || code === 3) {
          prompt(
            "Sorry, something went wrong, please try again or try again later.",
            msg
          );
        }
      },
      {
        enableHighAccuracy: true,
        timeout: 5000,
      }
    );
    // this.setState({
    //   userLocation: response,
    // });
    return response;
  };

  fetchTimes = async () => {
    const { userLocation, method, month, year } = this.state;
    const { lattitude, longitude } = userLocation;
    if (!lattitude) {
      let ress = await this.getUserLocation();
      console.log("ress: ", ress);
      const url = `http://api.address?latitude=${lattitude}&longitude=${longitude}&method=${method}&month=${month}&year=${year}`;
      console.log("url:", url);
      const resp = await fetch(url);
     ....

Несмотря на то, что getUserLocation вызывается с использованием ключевого слова await, консоль сообщает, что вызов fetch сделан до того, как пользователю будет предложен доступ к местоположению, и это местоположение будет возвращено функции fetchTimes.Почему он не ждет, пока getUserLocation вернет местоположение, прежде чем приступить к вызову fetch?

Спасибо

...