最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Ionic confirmation alert: await click on alert before continuing - Stack Overflow

programmeradmin4浏览0评论

I have a function that runs some arbitrary code, called calculate(). I have an if condition and if it is true I present an ionic confirm alert.

I can get the confirm alert to popup, however, I am trying to use async/await to wait for a response in the confirmation, but my understanding of async/await must be wrong. Here is basically what I am doing:

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();
  }
}

When the confirmation pops up, the remainder of the calculate() fn continues. I want it to wait for the confirmation response.

Any ideas of how to achieve this?

I have a function that runs some arbitrary code, called calculate(). I have an if condition and if it is true I present an ionic confirm alert.

I can get the confirm alert to popup, however, I am trying to use async/await to wait for a response in the confirmation, but my understanding of async/await must be wrong. Here is basically what I am doing:

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();
  }
}

When the confirmation pops up, the remainder of the calculate() fn continues. I want it to wait for the confirmation response.

Any ideas of how to achieve this?

Share Improve this question edited Jan 11, 2019 at 21:07 Ronnie asked Jan 11, 2019 at 20:00 RonnieRonnie 11.2k22 gold badges84 silver badges142 bronze badges 2
  • this tutorial should help you to understand async await youtube./watch?v=vn3tm0quoqE – Juan Lozoya Commented Jan 11, 2019 at 22:24
  • thanks @JuanLozoya that was a well done video and explained a lot of things – Ronnie Commented Jan 11, 2019 at 22:44
Add a ment  | 

2 Answers 2

Reset to default 14

I figured it out! I needed to first set the await to a constant and then wrap the dialog in a promise rather than return an individual promise per response.

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();
    });
  }
}

I needed to figure this out recently also. For future readers, I think it is a bit simpler though to return the onDidDismiss() promise and check the role for whatever button was selected. Also, you can check for the 'backdrop' role in case they canceled by clicking the background.

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();
    });
}
发布评论

评论列表(0)

  1. 暂无评论