我的等待语法无法正常工作

时间:2018-01-28 10:51:18

标签: angular typescript ionic-framework

正如您在下面的代码中看到的,首先我要求用户打开位置,然后我获得用户的地理位置。我应该只是在用户接受或拒绝位置许可时才会这样做。

这样的答案是未完成的,我得到了“答案是没有或未完成”的控制台日志部分。我需要等待答案,如果我在代码中继续得到答案。

     async locationReq() {
    this.locationAccuracy.canRequest().then((canRequest: boolean) => {

      if (canRequest) {
        this.locationAccuracy.request(this.locationAccuracy.REQUEST_PRIORITY_HIGH_ACCURACY).then(
          (success) => {
            if (success.message == this.locationAccuracy.SUCCESS_USER_AGREED || success.message==this.locationAccuracy.ERROR_USER_DISAGREED) {
              this.answer=true;
            }
            else{
              this.answer=false;
              alert("Hiba történt. Kérlek próbáld újra!");
            }

          });
      }

      console.log(this.answer);
    }).then(() => {

      if (this.answer == true) {
        console.log("answer is true");
        this.plt.ready().then(() => {
          let loader = this.loadingCtrl.create({
            content: "Helyadatok lekérdezése..."
          });
          loader.present();
          var options = {
            timeout: 15000
          };

          this.geolocation.getCurrentPosition(options).then(resp => {
            this.lat = resp.coords.latitude;
            this.lang = resp.coords.longitude;
            loader.dismiss();
            this.mapLoadingPresent();
          }).catch(() => {
            this.lat = 47.49801;
            this.lang = 19.03991;
            loader.dismiss();
            this.mapLoadingPresent();
            this.presentToast();
          });

        })
      } else {
        console.log("answer is no or undefinied");
      }
    });

  }

1 个答案:

答案 0 :(得分:0)

我重构了您的代码以使用async/await语法。在任何this.answer似乎都是undefined,因为canRequest()调用已返回false

const canRequest = await this.locationAccuracy.canRequest();

if (canRequest) {
  const success = await this.locationAccuracy.request(this.locationAccuracy.REQUEST_PRIORITY_HIGH_ACCURACY);

  if (success.message == this.locationAccuracy.SUCCESS_USER_AGREED || success.message == this.locationAccuracy.ERROR_USER_DISAGREED) {
    this.answer = true;
  } else {
    this.answer = false;
    alert('Hiba történt. Kérlek próbáld újra!');
  }
}

// If `this.answer` is undefined  it means `canRequest()` returned `false`
// otherwise it will be `true` or `false`
console.log(this.answer);

if (this.answer == true) {
  console.log('answer is true');
  await this.plt.ready();

  const loader = this.loadingCtrl.create({ content: 'Helyadatok lekérdezése...' });
  loader.present(); // Not sure if this must be awaited..

  const options = { timeout: 15000 };

  try {
    const resp = await this.geolocation.getCurrentPosition(options);
    this.lat = resp.coords.latitude;
    this.lang = resp.coords.longitude;
  } catch (err) {
    this.lat = 47.49801;
    this.lang = 19.03991;
    this.presentToast(); // Not sure if this must be awaited..
  } finally {
    loader.dismiss(); // Not sure if this must be awaited..
    this.mapLoadingPresent(); // Not sure if this must be awaited..
  }
} else {
  console.log('answer is no or undefinied');
}