离子确认警报:等待单击警报,然后继续

时间:2019-01-11 20:00:52

标签: javascript angular ionic-framework ecmascript-6

我有一个运行一些任意代码的函数,称为calculate()。我有if病情,如果是true,我会发出离子确认警报。

我可以弹出确认警报,但是,我试图使用async / await等待确认中的响应,但是我对async / await的理解一定是错误的。基本上,这就是我在做什么:

import { AlertController } from '@ionic/angular';

export class Calculator {
  private cancelResults:boolean = false;

  constructor(private alertController:AlertController) {}

  async calculate() {
    // If my condition is true.
    if (true) {
      // show the user a confirm alert.
      await this.warn();

      // break out of function since they hit cancel.
      if (this.cancelResults) return;
    }

    // The user hit Okay, continue with this function.
  }

  async warn() {
    const alert = await this.alertController.create({
      header: 'confirm',
      message: 'my message',
      buttons: [
        {
          text: 'Cancel',
          role: 'cancel',
          handler: (blah) => {
            console.log('they hit cancel');
            this.cancelResults = true;
            return new Promise(resolve => setTimeout(resolve, 2000));
          }
        }, {
          text: 'Okay',
          handler: () => {
            console.log('they hit ok');
            return new Promise(resolve => setTimeout(resolve, 2000));
          }
        }
      ]
    });

    await alert.present();
  }
}

弹出确认框时,calculate() fn的其余部分继续。我希望它等待确认响应。

关于如何实现这一目标的任何想法?

2 个答案:

答案 0 :(得分:3)

我知道了!我需要先将await设置为一个常量,然后将对话框包装在一个Promise中,而不是根据每个响应返回单个Promise。

import { AlertController } from '@ionic/angular';

export class Calculator {
  constructor(private alertController:AlertController) {}

  async calculate() {
    // If my condition is true.
    if (true) {
      // show the user a confirm alert.
      const confirmation = await this.warn();
      // break out of function since they hit cancel.
      if (!confirmation) return;
    }

    // The user hit Okay, continue with this function.
  }

  async warn() {
    return new Promise(async (resolve) => {
      const confirm = await this.alertController.create({
        header: 'confirm',
        message: 'my message',
        buttons: [
          {
            text: 'Cancel',
            role: 'cancel',
            handler: () => {
              return resolve(false);
            },
          },
          {
            text: 'OK',
            handler: () => {
              return resolve(true);
            },
          },
        ],
      });

      await confirm.present();
    });
  }
}

答案 1 :(得分:1)

我最近也需要弄清楚这一点。对于将来的读者,我认为返回onDidDismiss()承诺并检查所选按钮的作用要简单一些。另外,您可以通过单击背景来检查“背景”角色是否被取消。

const confirmation = await this.myAlert('Confirm', 'Message');

if (confirmation.role === 'cancel' || confirmation.role === 'backdrop') {
  // clean up code
  return;
}

myAlert(header: string, message: string) {
  return this.alertCtrl
    .create({
      header: header,
      message: message,
      buttons: [
        {
          text: 'Cancel',
          role: 'cancel',
        },
        {
          text: 'Okay',
        },
      ],
    })
    .then((alertEl) => {
      alertEl.present();
      return alertEl.onDidDismiss();
    });
}